Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c2fbd37

Browse files
committed
feat: add solutions to lc problem: No.0415
No.0415.Add Strings
1 parent f626e96 commit c2fbd37

File tree

8 files changed

+157
-119
lines changed

8 files changed

+157
-119
lines changed

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

Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
**方法一:双指针**
53+
54+
我们用两个指针 $i$ 和 $j$ 分别指向两个字符串的末尾,从末尾开始逐位相加。每次取出对应位的数字 $a$ 和 $b,ドル计算它们的和 $a + b + c,ドル其中 $c$ 表示上一次相加的进位,最后将 $a + b + c$ 的个位数添加到追加到答案字符串的末尾,然后将 $a + b + c$ 的十位数作为进位 $c$ 的值,循环此过程直至两个字符串的指针都已经指向了字符串的开头并且进位 $c$ 的值为 0ドル$。
55+
56+
最后将答案字符串反转并返回即可。
57+
58+
时间复杂度 $O(max(m, n)),ドル其中 $m$ 和 $n$ 分别是两个字符串的长度。忽略答案字符串的空间消耗,空间复杂度 $O(1)$。
59+
5260
<!-- tabs:start -->
5361

5462
### **Python3**
@@ -58,14 +66,16 @@
5866
```python
5967
class Solution:
6068
def addStrings(self, num1: str, num2: str) -> str:
61-
i, j, carry = len(num1) - 1, len(num2) - 1, 0
69+
i, j= len(num1) - 1, len(num2) - 1
6270
ans = []
63-
while i >= 0 or j >= 0 or carry:
64-
carry += (0 if i < 0 else int(num1[i])) + (0 if j < 0 else int(num2[j]))
65-
carry, v = divmod(carry, 10)
71+
c = 0
72+
while i >= 0 or j >= 0 or c:
73+
a = 0 if i < 0 else int(num1[i])
74+
b = 0 if j < 0 else int(num2[j])
75+
c, v = divmod(a + b + c, 10)
6676
ans.append(str(v))
6777
i, j = i - 1, j - 1
68-
return ''.join(ans[::-1])
78+
return "".join(ans[::-1])
6979
```
7080

7181
### **Java**
@@ -75,51 +85,34 @@ class Solution:
7585
```java
7686
class Solution {
7787
public String addStrings(String num1, String num2) {
88+
int i = num1.length() - 1, j = num2.length() - 1;
7889
StringBuilder ans = new StringBuilder();
79-
int i = num1.length() - 1, j = num2.length() - 1, carry = 0;
80-
for (; i >= 0 || j >= 0 || carry > 0; --i, --j) {
81-
carry += (i < 0 ? 0 : num1.charAt(i) - '0') + (j < 0 ? 0 : num2.charAt(j) - '0');
82-
ans.append(carry % 10);
83-
carry /= 10;
90+
for (int c = 0; i >= 0 || j >= 0 || c > 0; --i, --j) {
91+
int a = i < 0 ? 0 : num1.charAt(i) - '0';
92+
int b = j < 0 ? 0 : num2.charAt(j) - '0';
93+
c += a + b;
94+
ans.append(c % 10);
95+
c /= 10;
8496
}
8597
return ans.reverse().toString();
8698
}
8799
}
88100
```
89101

90-
### **JavaScript**
91-
92-
```js
93-
/**
94-
* @param {string} num1
95-
* @param {string} num2
96-
* @return {string}
97-
*/
98-
var addStrings = function (num1, num2) {
99-
let ans = [];
100-
let [i, j, carry] = [num1.length - 1, num2.length - 1, 0];
101-
for (; i >= 0 || j >= 0 || carry; --i, --j) {
102-
carry += i < 0 ? 0 : parseInt(num1.charAt(i), 10);
103-
carry += j < 0 ? 0 : parseInt(num2.charAt(j), 10);
104-
ans.push(carry % 10);
105-
carry = Math.floor(carry / 10);
106-
}
107-
return ans.reverse().join('');
108-
};
109-
```
110-
111102
### **C++**
112103

113104
```cpp
114105
class Solution {
115106
public:
116107
string addStrings(string num1, string num2) {
108+
int i = num1.size() - 1, j = num2.size() - 1;
117109
string ans;
118-
int i = num1.size() - 1, j = num2.size() - 1, carry = 0;
119-
for (; i >= 0 || j >= 0 || carry; --i, --j) {
120-
carry += (i < 0 ? 0 : num1[i] - '0') + (j < 0 ? 0 : num2[j] - '0');
121-
ans += to_string(carry % 10);
122-
carry /= 10;
110+
for (int c = 0; i >= 0 || j >= 0 || c; --i, --j) {
111+
int a = i < 0 ? 0 : num1[i] - '0';
112+
int b = j < 0 ? 0 : num2[j] - '0';
113+
c += a + b;
114+
ans += to_string(c % 10);
115+
c /= 10;
123116
}
124117
reverse(ans.begin(), ans.end());
125118
return ans;
@@ -131,22 +124,47 @@ public:
131124
132125
```go
133126
func addStrings(num1 string, num2 string) string {
134-
ans := ""
135-
i, j, carry := len(num1)-1, len(num2)-1, 0
136-
for ; i >= 0 || j >= 0 || carry != 0; i, j = i-1, j-1 {
127+
i, j := len(num1)-1, len(num2)-1
128+
ans := []byte{}
129+
for c := 0; i >= 0 || j >= 0 || c > 0; i, j = i-1, j-1 {
137130
if i >= 0 {
138-
carry += int(num1[i] - '0')
131+
c += int(num1[i] - '0')
139132
}
140133
if j >= 0 {
141-
carry += int(num2[j] - '0')
134+
c += int(num2[j] - '0')
142135
}
143-
ans = strconv.Itoa(carry%10) + ans
144-
carry /= 10
136+
ans = append(ans, byte(c%10+'0'))
137+
c /= 10
145138
}
146-
return ans
139+
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
140+
ans[i], ans[j] = ans[j], ans[i]
141+
}
142+
return string(ans)
147143
}
148144
```
149145

146+
### **JavaScript**
147+
148+
```js
149+
/**
150+
* @param {string} num1
151+
* @param {string} num2
152+
* @return {string}
153+
*/
154+
var addStrings = function (num1, num2) {
155+
let i = num1.length - 1;
156+
let j = num2.length - 1;
157+
const ans = [];
158+
for (let c = 0; i >= 0 || j >= 0 || c; --i, --j) {
159+
c += i < 0 ? 0 : parseInt(num1.charAt(i), 10);
160+
c += j < 0 ? 0 : parseInt(num2.charAt(j), 10);
161+
ans.push(c % 10);
162+
c = Math.floor(c / 10);
163+
}
164+
return ans.reverse().join('');
165+
};
166+
```
167+
150168
### **TypeScript**
151169

152170
```ts

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

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -48,66 +48,51 @@
4848
```python
4949
class Solution:
5050
def addStrings(self, num1: str, num2: str) -> str:
51-
i, j, carry = len(num1) - 1, len(num2) - 1, 0
51+
i, j= len(num1) - 1, len(num2) - 1
5252
ans = []
53-
while i >= 0 or j >= 0 or carry:
54-
carry += (0 if i < 0 else int(num1[i])) + (0 if j < 0 else int(num2[j]))
55-
carry, v = divmod(carry, 10)
53+
c = 0
54+
while i >= 0 or j >= 0 or c:
55+
a = 0 if i < 0 else int(num1[i])
56+
b = 0 if j < 0 else int(num2[j])
57+
c, v = divmod(a + b + c, 10)
5658
ans.append(str(v))
5759
i, j = i - 1, j - 1
58-
return ''.join(ans[::-1])
60+
return "".join(ans[::-1])
5961
```
6062

6163
### **Java**
6264

6365
```java
6466
class Solution {
6567
public String addStrings(String num1, String num2) {
68+
int i = num1.length() - 1, j = num2.length() - 1;
6669
StringBuilder ans = new StringBuilder();
67-
int i = num1.length() - 1, j = num2.length() - 1, carry = 0;
68-
for (; i >= 0 || j >= 0 || carry > 0; --i, --j) {
69-
carry += (i < 0 ? 0 : num1.charAt(i) - '0') + (j < 0 ? 0 : num2.charAt(j) - '0');
70-
ans.append(carry % 10);
71-
carry /= 10;
70+
for (int c = 0; i >= 0 || j >= 0 || c > 0; --i, --j) {
71+
int a = i < 0 ? 0 : num1.charAt(i) - '0';
72+
int b = j < 0 ? 0 : num2.charAt(j) - '0';
73+
c += a + b;
74+
ans.append(c % 10);
75+
c /= 10;
7276
}
7377
return ans.reverse().toString();
7478
}
7579
}
7680
```
7781

78-
### **JavaScript**
79-
80-
```js
81-
/**
82-
* @param {string} num1
83-
* @param {string} num2
84-
* @return {string}
85-
*/
86-
var addStrings = function (num1, num2) {
87-
let ans = [];
88-
let [i, j, carry] = [num1.length - 1, num2.length - 1, 0];
89-
for (; i >= 0 || j >= 0 || carry; --i, --j) {
90-
carry += i < 0 ? 0 : parseInt(num1.charAt(i), 10);
91-
carry += j < 0 ? 0 : parseInt(num2.charAt(j), 10);
92-
ans.push(carry % 10);
93-
carry = Math.floor(carry / 10);
94-
}
95-
return ans.reverse().join('');
96-
};
97-
```
98-
9982
### **C++**
10083

10184
```cpp
10285
class Solution {
10386
public:
10487
string addStrings(string num1, string num2) {
88+
int i = num1.size() - 1, j = num2.size() - 1;
10589
string ans;
106-
int i = num1.size() - 1, j = num2.size() - 1, carry = 0;
107-
for (; i >= 0 || j >= 0 || carry; --i, --j) {
108-
carry += (i < 0 ? 0 : num1[i] - '0') + (j < 0 ? 0 : num2[j] - '0');
109-
ans += to_string(carry % 10);
110-
carry /= 10;
90+
for (int c = 0; i >= 0 || j >= 0 || c; --i, --j) {
91+
int a = i < 0 ? 0 : num1[i] - '0';
92+
int b = j < 0 ? 0 : num2[j] - '0';
93+
c += a + b;
94+
ans += to_string(c % 10);
95+
c /= 10;
11196
}
11297
reverse(ans.begin(), ans.end());
11398
return ans;
@@ -119,22 +104,47 @@ public:
119104
120105
```go
121106
func addStrings(num1 string, num2 string) string {
122-
ans := ""
123-
i, j, carry := len(num1)-1, len(num2)-1, 0
124-
for ; i >= 0 || j >= 0 || carry != 0; i, j = i-1, j-1 {
107+
i, j := len(num1)-1, len(num2)-1
108+
ans := []byte{}
109+
for c := 0; i >= 0 || j >= 0 || c > 0; i, j = i-1, j-1 {
125110
if i >= 0 {
126-
carry += int(num1[i] - '0')
111+
c += int(num1[i] - '0')
127112
}
128113
if j >= 0 {
129-
carry += int(num2[j] - '0')
114+
c += int(num2[j] - '0')
130115
}
131-
ans = strconv.Itoa(carry%10) + ans
132-
carry /= 10
116+
ans = append(ans, byte(c%10+'0'))
117+
c /= 10
133118
}
134-
return ans
119+
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
120+
ans[i], ans[j] = ans[j], ans[i]
121+
}
122+
return string(ans)
135123
}
136124
```
137125

126+
### **JavaScript**
127+
128+
```js
129+
/**
130+
* @param {string} num1
131+
* @param {string} num2
132+
* @return {string}
133+
*/
134+
var addStrings = function (num1, num2) {
135+
let i = num1.length - 1;
136+
let j = num2.length - 1;
137+
const ans = [];
138+
for (let c = 0; i >= 0 || j >= 0 || c; --i, --j) {
139+
c += i < 0 ? 0 : parseInt(num1.charAt(i), 10);
140+
c += j < 0 ? 0 : parseInt(num2.charAt(j), 10);
141+
ans.push(c % 10);
142+
c = Math.floor(c / 10);
143+
}
144+
return ans.reverse().join('');
145+
};
146+
```
147+
138148
### **TypeScript**
139149

140150
```ts

‎solution/0400-0499/0415.Add Strings/Solution.cpp‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
class Solution {
22
public:
33
string addStrings(string num1, string num2) {
4+
int i = num1.size() - 1, j = num2.size() - 1;
45
string ans;
5-
int i = num1.size() - 1, j = num2.size() - 1, carry = 0;
6-
for (; i >= 0 || j >= 0 || carry; --i, --j) {
7-
carry += (i < 0 ? 0 : num1[i] - '0') + (j < 0 ? 0 : num2[j] - '0');
8-
ans += to_string(carry % 10);
9-
carry /= 10;
6+
for (int c = 0; i >= 0 || j >= 0 || c; --i, --j) {
7+
int a = i < 0 ? 0 : num1[i] - '0';
8+
int b = j < 0 ? 0 : num2[j] - '0';
9+
c += a + b;
10+
ans += to_string(c % 10);
11+
c /= 10;
1012
}
1113
reverse(ans.begin(), ans.end());
1214
return ans;
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
func addStrings(num1 string, num2 string) string {
2-
ans:= ""
3-
i, j, carry:= len(num1)-1, len(num2)-1, 0
4-
for ; i >= 0 || j >= 0 || carry!= 0; i, j = i-1, j-1 {
2+
i, j:= len(num1)-1, len(num2)-1
3+
ans:= []byte{}
4+
for c:=0; i >= 0 || j >= 0 || c> 0; i, j = i-1, j-1 {
55
if i >= 0 {
6-
carry += int(num1[i] - '0')
6+
c += int(num1[i] - '0')
77
}
88
if j >= 0 {
9-
carry += int(num2[j] - '0')
9+
c += int(num2[j] - '0')
1010
}
11-
ans = strconv.Itoa(carry%10) +ans
12-
carry /= 10
11+
ans = append(ans, byte(c%10+'0'))
12+
c /= 10
1313
}
14-
return ans
14+
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
15+
ans[i], ans[j] = ans[j], ans[i]
16+
}
17+
return string(ans)
1518
}

‎solution/0400-0499/0415.Add Strings/Solution.java‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
class Solution {
22
public String addStrings(String num1, String num2) {
3+
int i = num1.length() - 1, j = num2.length() - 1;
34
StringBuilder ans = new StringBuilder();
4-
int i = num1.length() - 1, j = num2.length() - 1, carry = 0;
5-
for (; i >= 0 || j >= 0 || carry > 0; --i, --j) {
6-
carry += (i < 0 ? 0 : num1.charAt(i) - '0') + (j < 0 ? 0 : num2.charAt(j) - '0');
7-
ans.append(carry % 10);
8-
carry /= 10;
5+
for (int c = 0; i >= 0 || j >= 0 || c > 0; --i, --j) {
6+
int a = i < 0 ? 0 : num1.charAt(i) - '0';
7+
int b = j < 0 ? 0 : num2.charAt(j) - '0';
8+
c += a + b;
9+
ans.append(c % 10);
10+
c /= 10;
911
}
1012
return ans.reverse().toString();
1113
}

‎solution/0400-0499/0415.Add Strings/Solution.js‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
* @return {string}
55
*/
66
var addStrings = function (num1, num2) {
7-
let ans = [];
8-
let [i, j, carry] = [num1.length - 1, num2.length - 1, 0];
9-
for (; i >= 0 || j >= 0 || carry; --i, --j) {
10-
carry += i < 0 ? 0 : parseInt(num1.charAt(i), 10);
11-
carry += j < 0 ? 0 : parseInt(num2.charAt(j), 10);
12-
ans.push(carry % 10);
13-
carry = Math.floor(carry / 10);
7+
let i = num1.length - 1;
8+
let j = num2.length - 1;
9+
const ans = [];
10+
for (let c = 0; i >= 0 || j >= 0 || c; --i, --j) {
11+
c += i < 0 ? 0 : parseInt(num1.charAt(i), 10);
12+
c += j < 0 ? 0 : parseInt(num2.charAt(j), 10);
13+
ans.push(c % 10);
14+
c = Math.floor(c / 10);
1415
}
1516
return ans.reverse().join('');
1617
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /