forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_415.java
34 lines (31 loc) · 967 Bytes
/
_415.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.fishercoder.solutions;
public class _415 {
public static class Solution1 {
/**
* My completely original solution on 10/14/2021.
*/
public String addStrings(String num1, String num2) {
StringBuilder sb = new StringBuilder();
int i = num1.length() - 1;
int j = num2.length() - 1;
int carry = 0;
while (i >= 0 || j >= 0) {
int sum = carry;
if (i >= 0) {
sum += Character.getNumericValue(num1.charAt(i));
}
if (j >= 0) {
sum += Character.getNumericValue(num2.charAt(j));
}
sb.append(sum % 10);
carry = sum / 10;
i--;
j--;
}
if (carry > 0) {
sb.append(carry);
}
return sb.reverse().toString();
}
}
}