We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent f5312e9 commit e10638bCopy full SHA for e10638b
415. Add Strings
@@ -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