Skip to content

Commit 401d2bb

Browse files
committed
feat: update solutions to lc problem: No.1085
No.1085.Sum of Digits in the Minimum Number
1 parent 78310e3 commit 401d2bb

File tree

10 files changed

+54
-48
lines changed

10 files changed

+54
-48
lines changed

solution/1000-1099/1078.Occurrences After Bigram/README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,10 @@
5555
```python
5656
class Solution:
5757
def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
58-
words = text.split(' ')
58+
ws = text.split()
59+
n = len(ws)
5960
return [
60-
words[i + 2]
61-
for i in range(len(words) - 2)
62-
if words[i] == first and words[i + 1] == second
61+
ws[i + 2] for i in range(n - 2) if ws[i] == first and ws[i + 1] == second
6362
]
6463
```
6564

solution/1000-1099/1078.Occurrences After Bigram/README_EN.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@
3636
```python
3737
class Solution:
3838
def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
39-
words = text.split(' ')
39+
ws = text.split()
40+
n = len(ws)
4041
return [
41-
words[i + 2]
42-
for i in range(len(words) - 2)
43-
if words[i] == first and words[i + 1] == second
42+
ws[i + 2] for i in range(n - 2) if ws[i] == first and ws[i + 1] == second
4443
]
4544
```
4645

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class Solution:
22
def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
3-
words = text.split(' ')
3+
ws = text.split()
4+
n = len(ws)
45
return [
5-
words[i + 2]
6-
for i in range(len(words) - 2)
7-
if words[i] == first and words[i + 1] == second
6+
ws[i + 2] for i in range(n - 2) if ws[i] == first and ws[i + 1] == second
87
]

solution/1000-1099/1085.Sum of Digits in the Minimum Number/README.md

+18-12
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:模拟**
49+
50+
我们先找到数组中的最小值,记为 $x$。然后计算 $x$ 的各个数位上的数字之和,记为 $s$。最后判断 $s$ 是否为奇数,若是则返回 $0$,否则返回 $1$。
51+
52+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度。
53+
4854
<!-- tabs:start -->
4955

5056
### **Python3**
@@ -59,7 +65,7 @@ class Solution:
5965
while x:
6066
s += x % 10
6167
x //= 10
62-
return 0 if s % 2 else 1
68+
return s & 1 ^ 1
6369
```
6470

6571
### **Java**
@@ -69,16 +75,15 @@ class Solution:
6975
```java
7076
class Solution {
7177
public int sumOfDigits(int[] nums) {
72-
int x = nums[0];
78+
int x = 100;
7379
for (int v : nums) {
7480
x = Math.min(x, v);
7581
}
7682
int s = 0;
77-
while (x != 0) {
83+
for (; x > 0; x /= 10) {
7884
s += x % 10;
79-
x /= 10;
8085
}
81-
return 1 - s % 2;
86+
return s & 1 ^ 1;
8287
}
8388
}
8489
```
@@ -89,11 +94,12 @@ class Solution {
8994
class Solution {
9095
public:
9196
int sumOfDigits(vector<int>& nums) {
92-
int x = nums[0];
93-
for (int& v : nums) x = min(x, v);
97+
int x = *min_element(nums.begin(), nums.end());
9498
int s = 0;
95-
for (; x != 0; x /= 10) s += x % 10;
96-
return 1 - s % 2;
99+
for (; x > 0; x /= 10) {
100+
s += x % 10;
101+
}
102+
return s & 1 ^ 1;
97103
}
98104
};
99105
```
@@ -102,17 +108,17 @@ public:
102108
103109
```go
104110
func sumOfDigits(nums []int) int {
105-
x := nums[0]
111+
x := 100
106112
for _, v := range nums {
107113
if v < x {
108114
x = v
109115
}
110116
}
111117
s := 0
112-
for ; x != 0; x /= 10 {
118+
for ; x > 0; x /= 10 {
113119
s += x % 10
114120
}
115-
return 1 - s%2
121+
return s&1 ^ 1
116122
}
117123
```
118124

solution/1000-1099/1085.Sum of Digits in the Minimum Number/README_EN.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,23 @@ class Solution:
4545
while x:
4646
s += x % 10
4747
x //= 10
48-
return 0 if s % 2 else 1
48+
return s & 1 ^ 1
4949
```
5050

5151
### **Java**
5252

5353
```java
5454
class Solution {
5555
public int sumOfDigits(int[] nums) {
56-
int x = nums[0];
56+
int x = 100;
5757
for (int v : nums) {
5858
x = Math.min(x, v);
5959
}
6060
int s = 0;
61-
while (x != 0) {
61+
for (; x > 0; x /= 10) {
6262
s += x % 10;
63-
x /= 10;
6463
}
65-
return 1 - s % 2;
64+
return s & 1 ^ 1;
6665
}
6766
}
6867
```
@@ -73,11 +72,12 @@ class Solution {
7372
class Solution {
7473
public:
7574
int sumOfDigits(vector<int>& nums) {
76-
int x = nums[0];
77-
for (int& v : nums) x = min(x, v);
75+
int x = *min_element(nums.begin(), nums.end());
7876
int s = 0;
79-
for (; x != 0; x /= 10) s += x % 10;
80-
return 1 - s % 2;
77+
for (; x > 0; x /= 10) {
78+
s += x % 10;
79+
}
80+
return s & 1 ^ 1;
8181
}
8282
};
8383
```
@@ -86,17 +86,17 @@ public:
8686
8787
```go
8888
func sumOfDigits(nums []int) int {
89-
x := nums[0]
89+
x := 100
9090
for _, v := range nums {
9191
if v < x {
9292
x = v
9393
}
9494
}
9595
s := 0
96-
for ; x != 0; x /= 10 {
96+
for ; x > 0; x /= 10 {
9797
s += x % 10
9898
}
99-
return 1 - s%2
99+
return s&1 ^ 1
100100
}
101101
```
102102

Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
class Solution {
22
public:
33
int sumOfDigits(vector<int>& nums) {
4-
int x = nums[0];
5-
for (int& v : nums) x = min(x, v);
4+
int x = *min_element(nums.begin(), nums.end());
65
int s = 0;
7-
for (; x != 0; x /= 10) s += x % 10;
8-
return 1 - s % 2;
6+
for (; x > 0; x /= 10) {
7+
s += x % 10;
8+
}
9+
return s & 1 ^ 1;
910
}
1011
};
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
func sumOfDigits(nums []int) int {
2-
x := nums[0]
2+
x := 100
33
for _, v := range nums {
44
if v < x {
55
x = v
66
}
77
}
88
s := 0
9-
for ; x != 0; x /= 10 {
9+
for ; x > 0; x /= 10 {
1010
s += x % 10
1111
}
12-
return 1 - s%2
12+
return s&1 ^ 1
1313
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
class Solution {
22
public int sumOfDigits(int[] nums) {
3-
int x = nums[0];
3+
int x = 100;
44
for (int v : nums) {
55
x = Math.min(x, v);
66
}
77
int s = 0;
8-
while (x != 0) {
8+
for (; x > 0; x /= 10) {
99
s += x % 10;
10-
x /= 10;
1110
}
12-
return 1 - s % 2;
11+
return s & 1 ^ 1;
1312
}
1413
}

solution/1000-1099/1085.Sum of Digits in the Minimum Number/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ def sumOfDigits(self, nums: List[int]) -> int:
55
while x:
66
s += x % 10
77
x //= 10
8-
return 0 if s % 2 else 1
8+
return s & 1 ^ 1

solution/main.py

+3
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,13 @@ def generate_summary(result):
134134
def refresh(result):
135135
"""update problems"""
136136
pattern = re.compile("src=\"(.*?)\"")
137+
skip_question_ids = {1599}
137138

138139
for question in result:
139140
front_question_id = question['frontend_question_id']
140141
print(front_question_id)
142+
if int(front_question_id) in skip_question_ids:
143+
continue
141144
title = question['title_cn']
142145
title_en = question['title_en']
143146

0 commit comments

Comments
 (0)