49
49
50
50
<!-- 这里可写通用的实现逻辑 -->
51
51
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
+
52
60
<!-- tabs:start -->
53
61
54
62
### ** Python3**
58
66
``` python
59
67
class Solution :
60
68
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
62
70
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 )
66
76
ans.append(str (v))
67
77
i, j = i - 1 , j - 1
68
- return ' ' .join(ans[::- 1 ])
78
+ return " " .join(ans[::- 1 ])
69
79
```
70
80
71
81
### ** Java**
@@ -75,51 +85,34 @@ class Solution:
75
85
``` java
76
86
class Solution {
77
87
public String addStrings (String num1 , String num2 ) {
88
+ int i = num1. length() - 1 , j = num2. length() - 1 ;
78
89
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 ;
84
96
}
85
97
return ans. reverse(). toString();
86
98
}
87
99
}
88
100
```
89
101
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
-
111
102
### ** C++**
112
103
113
104
``` cpp
114
105
class Solution {
115
106
public:
116
107
string addStrings(string num1, string num2) {
108
+ int i = num1.size() - 1, j = num2.size() - 1;
117
109
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;
123
116
}
124
117
reverse(ans.begin(), ans.end());
125
118
return ans;
@@ -131,22 +124,47 @@ public:
131
124
132
125
```go
133
126
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 {
137
130
if i >= 0 {
138
- carry += int(num1[i] - '0')
131
+ c += int(num1[i] - '0')
139
132
}
140
133
if j >= 0 {
141
- carry += int(num2[j] - '0')
134
+ c += int(num2[j] - '0')
142
135
}
143
- ans = strconv.Itoa(carry %10) + ans
144
- carry /= 10
136
+ ans = append(ans, byte(c %10+'0'))
137
+ c /= 10
145
138
}
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)
147
143
}
148
144
```
149
145
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
+
150
168
### ** TypeScript**
151
169
152
170
``` ts
0 commit comments