feat: update solutions to lc problem: No.1085

No.1085.Sum of Digits in the Minimum Number
This commit is contained in:
yanglbme 2023-03-05 23:15:19 +08:00
parent 78310e3487
commit 401d2bb9f9
10 changed files with 54 additions and 48 deletions

View File

@ -55,11 +55,10 @@
```python
class Solution:
def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
words = text.split(' ')
ws = text.split()
n = len(ws)
return [
words[i + 2]
for i in range(len(words) - 2)
if words[i] == first and words[i + 1] == second
ws[i + 2] for i in range(n - 2) if ws[i] == first and ws[i + 1] == second
]
```

View File

@ -36,11 +36,10 @@
```python
class Solution:
def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
words = text.split(' ')
ws = text.split()
n = len(ws)
return [
words[i + 2]
for i in range(len(words) - 2)
if words[i] == first and words[i + 1] == second
ws[i + 2] for i in range(n - 2) if ws[i] == first and ws[i + 1] == second
]
```

View File

@ -1,8 +1,7 @@
class Solution:
def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
words = text.split(' ')
ws = text.split()
n = len(ws)
return [
words[i + 2]
for i in range(len(words) - 2)
if words[i] == first and words[i + 1] == second
ws[i + 2] for i in range(n - 2) if ws[i] == first and ws[i + 1] == second
]

View File

@ -45,6 +45,12 @@
<!-- 这里可写通用的实现逻辑 -->
**方法一:模拟**
我们先找到数组中的最小值,记为 $x$。然后计算 $x$ 的各个数位上的数字之和,记为 $s$。最后判断 $s$ 是否为奇数,若是则返回 $0$,否则返回 $1$。
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度。
<!-- tabs:start -->
### **Python3**
@ -59,7 +65,7 @@ class Solution:
while x:
s += x % 10
x //= 10
return 0 if s % 2 else 1
return s & 1 ^ 1
```
### **Java**
@ -69,16 +75,15 @@ class Solution:
```java
class Solution {
public int sumOfDigits(int[] nums) {
int x = nums[0];
int x = 100;
for (int v : nums) {
x = Math.min(x, v);
}
int s = 0;
while (x != 0) {
for (; x > 0; x /= 10) {
s += x % 10;
x /= 10;
}
return 1 - s % 2;
return s & 1 ^ 1;
}
}
```
@ -89,11 +94,12 @@ class Solution {
class Solution {
public:
int sumOfDigits(vector<int>& nums) {
int x = nums[0];
for (int& v : nums) x = min(x, v);
int x = *min_element(nums.begin(), nums.end());
int s = 0;
for (; x != 0; x /= 10) s += x % 10;
return 1 - s % 2;
for (; x > 0; x /= 10) {
s += x % 10;
}
return s & 1 ^ 1;
}
};
```
@ -102,17 +108,17 @@ public:
```go
func sumOfDigits(nums []int) int {
x := nums[0]
x := 100
for _, v := range nums {
if v < x {
x = v
}
}
s := 0
for ; x != 0; x /= 10 {
for ; x > 0; x /= 10 {
s += x % 10
}
return 1 - s%2
return s&1 ^ 1
}
```

View File

@ -45,7 +45,7 @@ class Solution:
while x:
s += x % 10
x //= 10
return 0 if s % 2 else 1
return s & 1 ^ 1
```
### **Java**
@ -53,16 +53,15 @@ class Solution:
```java
class Solution {
public int sumOfDigits(int[] nums) {
int x = nums[0];
int x = 100;
for (int v : nums) {
x = Math.min(x, v);
}
int s = 0;
while (x != 0) {
for (; x > 0; x /= 10) {
s += x % 10;
x /= 10;
}
return 1 - s % 2;
return s & 1 ^ 1;
}
}
```
@ -73,11 +72,12 @@ class Solution {
class Solution {
public:
int sumOfDigits(vector<int>& nums) {
int x = nums[0];
for (int& v : nums) x = min(x, v);
int x = *min_element(nums.begin(), nums.end());
int s = 0;
for (; x != 0; x /= 10) s += x % 10;
return 1 - s % 2;
for (; x > 0; x /= 10) {
s += x % 10;
}
return s & 1 ^ 1;
}
};
```
@ -86,17 +86,17 @@ public:
```go
func sumOfDigits(nums []int) int {
x := nums[0]
x := 100
for _, v := range nums {
if v < x {
x = v
}
}
s := 0
for ; x != 0; x /= 10 {
for ; x > 0; x /= 10 {
s += x % 10
}
return 1 - s%2
return s&1 ^ 1
}
```

View File

@ -1,10 +1,11 @@
class Solution {
public:
int sumOfDigits(vector<int>& nums) {
int x = nums[0];
for (int& v : nums) x = min(x, v);
int x = *min_element(nums.begin(), nums.end());
int s = 0;
for (; x != 0; x /= 10) s += x % 10;
return 1 - s % 2;
for (; x > 0; x /= 10) {
s += x % 10;
}
return s & 1 ^ 1;
}
};

View File

@ -1,13 +1,13 @@
func sumOfDigits(nums []int) int {
x := nums[0]
x := 100
for _, v := range nums {
if v < x {
x = v
}
}
s := 0
for ; x != 0; x /= 10 {
for ; x > 0; x /= 10 {
s += x % 10
}
return 1 - s%2
return s&1 ^ 1
}

View File

@ -1,14 +1,13 @@
class Solution {
public int sumOfDigits(int[] nums) {
int x = nums[0];
int x = 100;
for (int v : nums) {
x = Math.min(x, v);
}
int s = 0;
while (x != 0) {
for (; x > 0; x /= 10) {
s += x % 10;
x /= 10;
}
return 1 - s % 2;
return s & 1 ^ 1;
}
}

View File

@ -5,4 +5,4 @@ class Solution:
while x:
s += x % 10
x //= 10
return 0 if s % 2 else 1
return s & 1 ^ 1

View File

@ -134,10 +134,13 @@ def generate_summary(result):
def refresh(result):
"""update problems"""
pattern = re.compile("src=\"(.*?)\"")
skip_question_ids = {1599}
for question in result:
front_question_id = question['frontend_question_id']
print(front_question_id)
if int(front_question_id) in skip_question_ids:
continue
title = question['title_cn']
title_en = question['title_en']