Skip to content

Commit 682126b

Browse files
committed
feat: add solutions to lc problem: No.0415
No.0415.Add Strings
1 parent 29a3850 commit 682126b

File tree

6 files changed

+152
-39
lines changed

6 files changed

+152
-39
lines changed

solution/0400-0499/0415.Add Strings/README.md

+54-13
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@
3232
```python
3333
class Solution:
3434
def addStrings(self, num1: str, num2: str) -> str:
35-
n1, n2 = len(num1) - 1, len(num2) - 1
35+
ans = []
36+
i, j = len(num1) - 1, len(num2) - 1
3637
carry = 0
37-
res = []
38-
while n1 >= 0 or n2 >= 0 or carry > 0:
39-
carry += (0 if n1 < 0 else int(num1[n1])) + (0 if n2 < 0 else int(num2[n2]))
40-
res.append(str(carry % 10))
38+
while i >= 0 or j >= 0 or carry:
39+
carry += (0 if i < 0 else int(num1[i])) + (0 if j < 0 else int(num2[j]))
40+
ans.append(str(carry % 10))
4141
carry //= 10
42-
n1, n2 = n1 - 1, n2 - 1
43-
return ''.join(res[::-1])
42+
i, j = i - 1, j - 1
43+
return ''.join(ans[::-1])
4444
```
4545

4646
### **Java**
@@ -50,15 +50,15 @@ class Solution:
5050
```java
5151
class Solution {
5252
public String addStrings(String num1, String num2) {
53-
int n1 = num1.length() - 1, n2 = num2.length() - 1;
53+
int i = num1.length() - 1, j = num2.length() - 1;
5454
int carry = 0;
55-
StringBuilder sb = new StringBuilder();
56-
while (n1 >= 0 || n2 >= 0 || carry > 0) {
57-
carry += (n1 < 0 ? 0 : num1.charAt(n1--) - '0') + (n2 < 0 ? 0 : num2.charAt(n2--) - '0');
58-
sb.append(carry % 10);
55+
StringBuilder ans = new StringBuilder();
56+
while (i >= 0 || j >= 0 || carry > 0) {
57+
carry += (i < 0 ? 0 : num1.charAt(i--) - '0') + (j < 0 ? 0 : num2.charAt(j--) - '0');
58+
ans.append(carry % 10);
5959
carry /= 10;
6060
}
61-
return sb.reverse().toString();
61+
return ans.reverse().toString();
6262
}
6363
}
6464
```
@@ -88,6 +88,47 @@ var addStrings = function (num1, num2) {
8888
};
8989
```
9090

91+
### **C++**
92+
93+
```cpp
94+
class Solution {
95+
public:
96+
string addStrings(string num1, string num2) {
97+
int i = num1.size() - 1, j = num2.size() - 1;
98+
int carry = 0;
99+
string ans;
100+
while (i >= 0 || j >= 0 || carry)
101+
{
102+
carry += (i < 0 ? 0 : num1[i--] - '0') + (j < 0 ? 0 : num2[j--] - '0');
103+
ans += to_string(carry % 10);
104+
carry /= 10;
105+
}
106+
reverse(ans.begin(), ans.end());
107+
return ans;
108+
}
109+
};
110+
```
111+
112+
### **Go**
113+
114+
```go
115+
func addStrings(num1 string, num2 string) string {
116+
carry := 0
117+
ans := ""
118+
for i, j := len(num1)-1, len(num2)-1; i >= 0 || j >= 0 || carry != 0; i, j = i-1, j-1 {
119+
if i >= 0 {
120+
carry += int(num1[i] - '0')
121+
}
122+
if j >= 0 {
123+
carry += int(num2[j] - '0')
124+
}
125+
ans = strconv.Itoa(carry%10) + ans
126+
carry /= 10
127+
}
128+
return ans
129+
}
130+
```
131+
91132
### **...**
92133

93134
```

solution/0400-0499/0415.Add Strings/README_EN.md

+54-13
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,31 @@
4949
```python
5050
class Solution:
5151
def addStrings(self, num1: str, num2: str) -> str:
52-
n1, n2 = len(num1) - 1, len(num2) - 1
52+
ans = []
53+
i, j = len(num1) - 1, len(num2) - 1
5354
carry = 0
54-
res = []
55-
while n1 >= 0 or n2 >= 0 or carry > 0:
56-
carry += (0 if n1 < 0 else int(num1[n1])) + (0 if n2 < 0 else int(num2[n2]))
57-
res.append(str(carry % 10))
55+
while i >= 0 or j >= 0 or carry:
56+
carry += (0 if i < 0 else int(num1[i])) + (0 if j < 0 else int(num2[j]))
57+
ans.append(str(carry % 10))
5858
carry //= 10
59-
n1, n2 = n1 - 1, n2 - 1
60-
return ''.join(res[::-1])
59+
i, j = i - 1, j - 1
60+
return ''.join(ans[::-1])
6161
```
6262

6363
### **Java**
6464

6565
```java
6666
class Solution {
6767
public String addStrings(String num1, String num2) {
68-
int n1 = num1.length() - 1, n2 = num2.length() - 1;
68+
int i = num1.length() - 1, j = num2.length() - 1;
6969
int carry = 0;
70-
StringBuilder sb = new StringBuilder();
71-
while (n1 >= 0 || n2 >= 0 || carry > 0) {
72-
carry += (n1 < 0 ? 0 : num1.charAt(n1--) - '0') + (n2 < 0 ? 0 : num2.charAt(n2--) - '0');
73-
sb.append(carry % 10);
70+
StringBuilder ans = new StringBuilder();
71+
while (i >= 0 || j >= 0 || carry > 0) {
72+
carry += (i < 0 ? 0 : num1.charAt(i--) - '0') + (j < 0 ? 0 : num2.charAt(j--) - '0');
73+
ans.append(carry % 10);
7474
carry /= 10;
7575
}
76-
return sb.reverse().toString();
76+
return ans.reverse().toString();
7777
}
7878
}
7979
```
@@ -103,6 +103,47 @@ var addStrings = function (num1, num2) {
103103
};
104104
```
105105

106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
string addStrings(string num1, string num2) {
112+
int i = num1.size() - 1, j = num2.size() - 1;
113+
int carry = 0;
114+
string ans;
115+
while (i >= 0 || j >= 0 || carry)
116+
{
117+
carry += (i < 0 ? 0 : num1[i--] - '0') + (j < 0 ? 0 : num2[j--] - '0');
118+
ans += to_string(carry % 10);
119+
carry /= 10;
120+
}
121+
reverse(ans.begin(), ans.end());
122+
return ans;
123+
}
124+
};
125+
```
126+
127+
### **Go**
128+
129+
```go
130+
func addStrings(num1 string, num2 string) string {
131+
carry := 0
132+
ans := ""
133+
for i, j := len(num1)-1, len(num2)-1; i >= 0 || j >= 0 || carry != 0; i, j = i-1, j-1 {
134+
if i >= 0 {
135+
carry += int(num1[i] - '0')
136+
}
137+
if j >= 0 {
138+
carry += int(num2[j] - '0')
139+
}
140+
ans = strconv.Itoa(carry%10) + ans
141+
carry /= 10
142+
}
143+
return ans
144+
}
145+
```
146+
106147
### **...**
107148

108149
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
string addStrings(string num1, string num2) {
4+
int i = num1.size() - 1, j = num2.size() - 1;
5+
int carry = 0;
6+
string ans;
7+
while (i >= 0 || j >= 0 || carry)
8+
{
9+
carry += (i < 0 ? 0 : num1[i--] - '0') + (j < 0 ? 0 : num2[j--] - '0');
10+
ans += to_string(carry % 10);
11+
carry /= 10;
12+
}
13+
reverse(ans.begin(), ans.end());
14+
return ans;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func addStrings(num1 string, num2 string) string {
2+
carry := 0
3+
ans := ""
4+
for i, j := len(num1)-1, len(num2)-1; i >= 0 || j >= 0 || carry != 0; i, j = i-1, j-1 {
5+
if i >= 0 {
6+
carry += int(num1[i] - '0')
7+
}
8+
if j >= 0 {
9+
carry += int(num2[j] - '0')
10+
}
11+
ans = strconv.Itoa(carry%10) + ans
12+
carry /= 10
13+
}
14+
return ans
15+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution {
22
public String addStrings(String num1, String num2) {
3-
int n1 = num1.length() - 1, n2 = num2.length() - 1;
3+
int i = num1.length() - 1, j = num2.length() - 1;
44
int carry = 0;
5-
StringBuilder sb = new StringBuilder();
6-
while (n1 >= 0 || n2 >= 0 || carry > 0) {
7-
carry += (n1 < 0 ? 0 : num1.charAt(n1--) - '0') + (n2 < 0 ? 0 : num2.charAt(n2--) - '0');
8-
sb.append(carry % 10);
5+
StringBuilder ans = new StringBuilder();
6+
while (i >= 0 || j >= 0 || carry > 0) {
7+
carry += (i < 0 ? 0 : num1.charAt(i--) - '0') + (j < 0 ? 0 : num2.charAt(j--) - '0');
8+
ans.append(carry % 10);
99
carry /= 10;
1010
}
11-
return sb.reverse().toString();
11+
return ans.reverse().toString();
1212
}
1313
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution:
22
def addStrings(self, num1: str, num2: str) -> str:
3-
n1, n2 = len(num1) - 1, len(num2) - 1
3+
ans = []
4+
i, j = len(num1) - 1, len(num2) - 1
45
carry = 0
5-
res = []
6-
while n1 >= 0 or n2 >= 0 or carry > 0:
7-
carry += (0 if n1 < 0 else int(num1[n1])) + (0 if n2 < 0 else int(num2[n2]))
8-
res.append(str(carry % 10))
6+
while i >= 0 or j >= 0 or carry:
7+
carry += (0 if i < 0 else int(num1[i])) + (0 if j < 0 else int(num2[j]))
8+
ans.append(str(carry % 10))
99
carry //= 10
10-
n1, n2 = n1 - 1, n2 - 1
11-
return ''.join(res[::-1])
10+
i, j = i - 1, j - 1
11+
return ''.join(ans[::-1])

0 commit comments

Comments
 (0)