Skip to content

Commit 7be2ed1

Browse files
committed
feat: add solutions to lc/lcp problems
* lcp No.17.速算机器人 * lc No.1085.Sum of Digits in the Minimum Number
1 parent 0ee6b54 commit 7be2ed1

File tree

11 files changed

+252
-3
lines changed

11 files changed

+252
-3
lines changed

lcp/LCP 17. 速算机器人/README.md

+54-1
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,68 @@
4646
<!-- 这里可写当前语言的特殊实现逻辑 -->
4747

4848
```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
5058
```
5159

5260
### **Java**
5361

5462
<!-- 这里可写当前语言的特殊实现逻辑 -->
5563

5664
```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+
```
5796
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+
}
58111
```
59112

60113
### **...**
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
};
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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

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

+54-1
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,68 @@
5252
<!-- 这里可写当前语言的特殊实现逻辑 -->
5353

5454
```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
5663
```
5764

5865
### **Java**
5966

6067
<!-- 这里可写当前语言的特殊实现逻辑 -->
6168

6269
```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+
```
63100
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+
}
64117
```
65118

66119
### **...**

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

+54-1
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,66 @@
3838
### **Python3**
3939

4040
```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
4249
```
4350

4451
### **Java**
4552

4653
```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+
```
4784
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+
}
48101
```
49102

50103
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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

0 commit comments

Comments
 (0)