Skip to content

Commit a082a60

Browse files
authored
Create 415. Add Strings.java
1 parent 13e870b commit a082a60

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

415. Add Strings.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Solution {
2+
public String addStrings(String num1, String num2) {
3+
int i = num1.length()-1, j = num2.length()-1, carry = 0, num = 0, digit1 = 0, digit2 = 0;
4+
StringBuilder sb = new StringBuilder();
5+
6+
while (i >= 0 || j >= 0 || carry > 0) {
7+
digit1 = i < 0 ? 0 : (num1.charAt(i) - '0');
8+
digit2 = j < 0 ? 0 : (num2.charAt(j) - '0');
9+
num = digit1 + digit2 + carry;
10+
carry = num / 10;
11+
num %= 10;
12+
sb.insert(0, num);
13+
i--; j--;
14+
}
15+
16+
return sb.toString();
17+
}
18+
}

0 commit comments

Comments
 (0)