Skip to content

Commit e10638b

Browse files
authored
Create 415. Add Strings
1 parent f5312e9 commit e10638b

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

415. Add Strings

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Solution {
2+
public String addStrings(String num1, String num2) {
3+
StringBuilder sb = new StringBuilder();
4+
int carry = 0;
5+
// we need to deal with the last carry that might exist
6+
for(int i = num1.length() - 1, j = num2.length() - 1; i >= 0 || j >= 0 || carry == 1; i--, j--){
7+
int x = i < 0 ? 0 : num1.charAt(i) - '0';
8+
int y = j < 0 ? 0 : num2.charAt(j) - '0';
9+
sb.append((x + y + carry) % 10);
10+
carry = (x + y + carry) / 10;
11+
}
12+
return sb.reverse().toString();
13+
}
14+
}

0 commit comments

Comments
 (0)