File tree 11 files changed +252
-3
lines changed
solution/1000-1099/1085.Sum of Digits in the Minimum Number
11 files changed +252
-3
lines changed Original file line number Diff line number Diff line change 46
46
<!-- 这里可写当前语言的特殊实现逻辑 -->
47
47
48
48
``` python
49
-
49
+ class Solution :
50
+ def calculate (self , s : str ) -> int :
51
+ x, y = 1 , 0
52
+ for c in s:
53
+ if c == ' A' :
54
+ x = x * 2 + y
55
+ else :
56
+ y = y * 2 + x
57
+ return x + y
50
58
```
51
59
52
60
### ** Java**
53
61
54
62
<!-- 这里可写当前语言的特殊实现逻辑 -->
55
63
56
64
``` java
65
+ class Solution {
66
+ public int calculate (String s ) {
67
+ int x = 1 , y = 0 ;
68
+ for (char c : s. toCharArray()) {
69
+ if (c == ' A' ) {
70
+ x = x * 2 + y;
71
+ } else {
72
+ y = y * 2 + x;
73
+ }
74
+ }
75
+ return x + y;
76
+ }
77
+ }
78
+ ```
79
+
80
+ ### ** C++**
81
+
82
+ ``` cpp
83
+ class Solution {
84
+ public:
85
+ int calculate(string s) {
86
+ int x = 1, y = 0;
87
+ for (char& c : s)
88
+ {
89
+ if (c == 'A') x = x * 2 + y;
90
+ else y = y * 2 + x;
91
+ }
92
+ return x + y;
93
+ }
94
+ };
95
+ ```
57
96
97
+ ### **Go**
98
+
99
+ ```go
100
+ func calculate(s string) int {
101
+ x, y := 1, 0
102
+ for _, c := range s {
103
+ if c == 'A' {
104
+ x = x*2 + y
105
+ } else {
106
+ y = y*2 + x
107
+ }
108
+ }
109
+ return x + y
110
+ }
58
111
```
59
112
60
113
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int calculate (string s) {
4
+ int x = 1 , y = 0 ;
5
+ for (char & c : s)
6
+ {
7
+ if (c == ' A' ) x = x * 2 + y;
8
+ else y = y * 2 + x;
9
+ }
10
+ return x + y;
11
+ }
12
+ };
Original file line number Diff line number Diff line change
1
+ func calculate (s string ) int {
2
+ x , y := 1 , 0
3
+ for _ , c := range s {
4
+ if c == 'A' {
5
+ x = x * 2 + y
6
+ } else {
7
+ y = y * 2 + x
8
+ }
9
+ }
10
+ return x + y
11
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int calculate (String s ) {
3
+ int x = 1 , y = 0 ;
4
+ for (char c : s .toCharArray ()) {
5
+ if (c == 'A' ) {
6
+ x = x * 2 + y ;
7
+ } else {
8
+ y = y * 2 + x ;
9
+ }
10
+ }
11
+ return x + y ;
12
+ }
13
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def calculate (self , s : str ) -> int :
3
+ x , y = 1 , 0
4
+ for c in s :
5
+ if c == 'A' :
6
+ x = x * 2 + y
7
+ else :
8
+ y = y * 2 + x
9
+ return x + y
Original file line number Diff line number Diff line change 52
52
<!-- 这里可写当前语言的特殊实现逻辑 -->
53
53
54
54
``` python
55
-
55
+ class Solution :
56
+ def sumOfDigits (self , nums : List[int ]) -> int :
57
+ x = min (nums)
58
+ s = 0
59
+ while x:
60
+ s += x % 10
61
+ x //= 10
62
+ return 0 if s % 2 else 1
56
63
```
57
64
58
65
### ** Java**
59
66
60
67
<!-- 这里可写当前语言的特殊实现逻辑 -->
61
68
62
69
``` java
70
+ class Solution {
71
+ public int sumOfDigits (int [] nums ) {
72
+ int x = nums[0 ];
73
+ for (int v : nums) {
74
+ x = Math . min(x, v);
75
+ }
76
+ int s = 0 ;
77
+ while (x != 0 ) {
78
+ s += x % 10 ;
79
+ x /= 10 ;
80
+ }
81
+ return 1 - s % 2 ;
82
+ }
83
+ }
84
+ ```
85
+
86
+ ### ** C++**
87
+
88
+ ``` cpp
89
+ class Solution {
90
+ public:
91
+ int sumOfDigits(vector<int >& nums) {
92
+ int x = nums[ 0] ;
93
+ for (int& v : nums) x = min(x, v);
94
+ int s = 0;
95
+ for (; x != 0; x /= 10) s += x % 10;
96
+ return 1 - s % 2;
97
+ }
98
+ };
99
+ ```
63
100
101
+ ### **Go**
102
+
103
+ ```go
104
+ func sumOfDigits(nums []int) int {
105
+ x := nums[0]
106
+ for _, v := range nums {
107
+ if v < x {
108
+ x = v
109
+ }
110
+ }
111
+ s := 0
112
+ for ; x != 0; x /= 10 {
113
+ s += x % 10
114
+ }
115
+ return 1 - s%2
116
+ }
64
117
```
65
118
66
119
### ** ...**
Original file line number Diff line number Diff line change 38
38
### ** Python3**
39
39
40
40
``` python
41
-
41
+ class Solution :
42
+ def sumOfDigits (self , nums : List[int ]) -> int :
43
+ x = min (nums)
44
+ s = 0
45
+ while x:
46
+ s += x % 10
47
+ x //= 10
48
+ return 0 if s % 2 else 1
42
49
```
43
50
44
51
### ** Java**
45
52
46
53
``` java
54
+ class Solution {
55
+ public int sumOfDigits (int [] nums ) {
56
+ int x = nums[0 ];
57
+ for (int v : nums) {
58
+ x = Math . min(x, v);
59
+ }
60
+ int s = 0 ;
61
+ while (x != 0 ) {
62
+ s += x % 10 ;
63
+ x /= 10 ;
64
+ }
65
+ return 1 - s % 2 ;
66
+ }
67
+ }
68
+ ```
69
+
70
+ ### ** C++**
71
+
72
+ ``` cpp
73
+ class Solution {
74
+ public:
75
+ int sumOfDigits(vector<int >& nums) {
76
+ int x = nums[ 0] ;
77
+ for (int& v : nums) x = min(x, v);
78
+ int s = 0;
79
+ for (; x != 0; x /= 10) s += x % 10;
80
+ return 1 - s % 2;
81
+ }
82
+ };
83
+ ```
47
84
85
+ ### **Go**
86
+
87
+ ```go
88
+ func sumOfDigits(nums []int) int {
89
+ x := nums[0]
90
+ for _, v := range nums {
91
+ if v < x {
92
+ x = v
93
+ }
94
+ }
95
+ s := 0
96
+ for ; x != 0; x /= 10 {
97
+ s += x % 10
98
+ }
99
+ return 1 - s%2
100
+ }
48
101
```
49
102
50
103
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int sumOfDigits (vector<int >& nums) {
4
+ int x = nums[0 ];
5
+ for (int & v : nums) x = min (x, v);
6
+ int s = 0 ;
7
+ for (; x != 0 ; x /= 10 ) s += x % 10 ;
8
+ return 1 - s % 2 ;
9
+ }
10
+ };
Original file line number Diff line number Diff line change
1
+ func sumOfDigits (nums []int ) int {
2
+ x := nums [0 ]
3
+ for _ , v := range nums {
4
+ if v < x {
5
+ x = v
6
+ }
7
+ }
8
+ s := 0
9
+ for ; x != 0 ; x /= 10 {
10
+ s += x % 10
11
+ }
12
+ return 1 - s % 2
13
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int sumOfDigits (int [] nums ) {
3
+ int x = nums [0 ];
4
+ for (int v : nums ) {
5
+ x = Math .min (x , v );
6
+ }
7
+ int s = 0 ;
8
+ while (x != 0 ) {
9
+ s += x % 10 ;
10
+ x /= 10 ;
11
+ }
12
+ return 1 - s % 2 ;
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def sumOfDigits (self , nums : List [int ]) -> int :
3
+ x = min (nums )
4
+ s = 0
5
+ while x :
6
+ s += x % 10
7
+ x //= 10
8
+ return 0 if s % 2 else 1
You can’t perform that action at this time.
0 commit comments