File tree 1 file changed +9
-14
lines changed 1 file changed +9
-14
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public String addStrings (String num1 , String num2 ) {
3
3
StringBuilder sb = new StringBuilder ();
4
- int idx1 = num1 .length () - 1 ;
5
- int idx2 = num2 .length () - 1 ;
4
+ int idxOne = num1 .length () - 1 ;
5
+ int idxTwo = num2 .length () - 1 ;
6
6
int carry = 0 ;
7
- while (idx1 >= 0 || idx2 >= 0 ) {
7
+ while (idxOne >= 0 || idxTwo >= 0 || carry > 0 ) {
8
8
int temp = carry ;
9
- if (idx1 >= 0 && idx2 >= 0 ) {
10
- temp += Character .getNumericValue (num1 .charAt (idx1 --)) + Character .getNumericValue (num2 .charAt (idx2 --));
11
- }
12
- else if (idx1 >= 0 && idx2 < 0 ) {
13
- temp += Character .getNumericValue (num1 .charAt (idx1 --));
14
- }
15
- else {
16
- temp += Character .getNumericValue (num2 .charAt (idx2 --));
9
+ if (idxOne >= 0 && idxTwo >= 0 ) {
10
+ temp += Character .getNumericValue (num1 .charAt (idxOne --)) + Character .getNumericValue (num2 .charAt (idxTwo --));
11
+ } else if (idxOne >= 0 && idxTwo < 0 ) {
12
+ temp += Character .getNumericValue (num1 .charAt (idxOne --));
13
+ } else if (idxOne < 0 && idxTwo >= 0 ) {
14
+ temp += Character .getNumericValue (num2 .charAt (idxTwo --));
17
15
}
18
16
carry = temp > 9 ? 1 : 0 ;
19
17
temp = temp > 9 ? temp % 10 : temp ;
20
18
sb .append (temp );
21
19
}
22
- if (carry > 0 ) {
23
- sb .append (carry );
24
- }
25
20
return sb .reverse ().toString ();
26
21
}
27
22
}
You can’t perform that action at this time.
0 commit comments