File tree Expand file tree Collapse file tree 4 files changed +66
-12
lines changed
solution/0400-0499/0415.Add Strings Expand file tree Collapse file tree 4 files changed +66
-12
lines changed Original file line number Diff line number Diff line change 2727<!-- 这里可写当前语言的特殊实现逻辑 -->
2828
2929``` 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 ])
3141```
3242
3343### ** Java**
3444
3545<!-- 这里可写当前语言的特殊实现逻辑 -->
3646
3747``` 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+ }
3961```
4062
4163### ** ...**
Original file line number Diff line number Diff line change 2929### ** Python3**
3030
3131``` 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 ])
3343```
3444
3545### ** Java**
3646
3747``` 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+ }
3961```
4062
4163### ** ...**
Original file line number Diff line number Diff line change 11class Solution {
22 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 ;
54 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 );
109 carry /= 10 ;
1110 }
12- return res .reverse ().toString ();
11+ return sb .reverse ().toString ();
1312 }
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