File tree 4 files changed +66
-12
lines changed
solution/0400-0499/0415.Add Strings
4 files changed +66
-12
lines changed Original file line number Diff line number Diff line change 27
27
<!-- 这里可写当前语言的特殊实现逻辑 -->
28
28
29
29
``` python
30
-
30
+ class Solution :
31
+ def addStrings (self , num1 : str , num2 : str ) -> str :
32
+ n1, n2 = len (num1) - 1 , len (num2) - 1
33
+ carry = 0
34
+ res = []
35
+ while n1 >= 0 or n2 >= 0 or carry > 0 :
36
+ carry += (0 if n1 < 0 else int (num1[n1])) + (0 if n2 < 0 else int (num2[n2]))
37
+ res.append(str (carry % 10 ))
38
+ carry //= 10
39
+ n1, n2 = n1 - 1 , n2 - 1
40
+ return ' ' .join(res[::- 1 ])
31
41
```
32
42
33
43
### ** Java**
34
44
35
45
<!-- 这里可写当前语言的特殊实现逻辑 -->
36
46
37
47
``` java
38
-
48
+ class Solution {
49
+ public String addStrings (String num1 , String num2 ) {
50
+ int n1 = num1. length() - 1 , n2 = num2. length() - 1 ;
51
+ int carry = 0 ;
52
+ StringBuilder sb = new StringBuilder ();
53
+ while (n1 >= 0 || n2 >= 0 || carry > 0 ) {
54
+ carry += (n1 < 0 ? 0 : num1. charAt(n1-- ) - ' 0' ) + (n2 < 0 ? 0 : num2. charAt(n2-- ) - ' 0' );
55
+ sb. append(carry % 10 );
56
+ carry /= 10 ;
57
+ }
58
+ return sb. reverse(). toString();
59
+ }
60
+ }
39
61
```
40
62
41
63
### ** ...**
Original file line number Diff line number Diff line change 29
29
### ** Python3**
30
30
31
31
``` python
32
-
32
+ class Solution :
33
+ def addStrings (self , num1 : str , num2 : str ) -> str :
34
+ n1, n2 = len (num1) - 1 , len (num2) - 1
35
+ carry = 0
36
+ res = []
37
+ while n1 >= 0 or n2 >= 0 or carry > 0 :
38
+ carry += (0 if n1 < 0 else int (num1[n1])) + (0 if n2 < 0 else int (num2[n2]))
39
+ res.append(str (carry % 10 ))
40
+ carry //= 10
41
+ n1, n2 = n1 - 1 , n2 - 1
42
+ return ' ' .join(res[::- 1 ])
33
43
```
34
44
35
45
### ** Java**
36
46
37
47
``` java
38
-
48
+ class Solution {
49
+ public String addStrings (String num1 , String num2 ) {
50
+ int n1 = num1. length() - 1 , n2 = num2. length() - 1 ;
51
+ int carry = 0 ;
52
+ StringBuilder sb = new StringBuilder ();
53
+ while (n1 >= 0 || n2 >= 0 || carry > 0 ) {
54
+ carry += (n1 < 0 ? 0 : num1. charAt(n1-- ) - ' 0' ) + (n2 < 0 ? 0 : num2. charAt(n2-- ) - ' 0' );
55
+ sb. append(carry % 10 );
56
+ carry /= 10 ;
57
+ }
58
+ return sb. reverse(). toString();
59
+ }
60
+ }
39
61
```
40
62
41
63
### ** ...**
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public String addStrings (String num1 , String num2 ) {
3
- int i = num1 .length () - 1 ;
4
- int j = num2 .length () - 1 ;
3
+ int n1 = num1 .length () - 1 , n2 = num2 .length () - 1 ;
5
4
int carry = 0 ;
6
- StringBuilder res = new StringBuilder ();
7
- while (i >= 0 || j >= 0 || carry != 0 ) {
8
- carry += (i >= 0 ? num1 .charAt (i --) - '0' : 0 ) + (j >= 0 ? num2 .charAt (j --) - '0' : 0 );
9
- res .append (carry % 10 );
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 );
10
9
carry /= 10 ;
11
10
}
12
- return res .reverse ().toString ();
11
+ return sb .reverse ().toString ();
13
12
}
14
- }
13
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def addStrings (self , num1 : str , num2 : str ) -> str :
3
+ n1 , n2 = len (num1 ) - 1 , len (num2 ) - 1
4
+ 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 ))
9
+ carry //= 10
10
+ n1 , n2 = n1 - 1 , n2 - 1
11
+ return '' .join (res [::- 1 ])
You can’t perform that action at this time.
0 commit comments