forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd Strings.java
More file actions
27 lines (27 loc) · 814 Bytes
/
Add Strings.java
File metadata and controls
27 lines (27 loc) · 814 Bytes
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
class Solution {
public String addStrings(String num1, String num2) {
StringBuilder sb = new StringBuilder();
int idx1 = num1.length() - 1;
int idx2 = num2.length() - 1;
int carry = 0;
while (idx1 >= 0 || idx2 >= 0) {
int temp = carry;
if (idx1 >= 0 && idx2 >= 0) {
temp += Character.getNumericValue(num1.charAt(idx1--)) + Character.getNumericValue(num2.charAt(idx2--));
}
else if (idx1 >= 0 && idx2 < 0) {
temp += Character.getNumericValue(num1.charAt(idx1--));
}
else {
temp += Character.getNumericValue(num2.charAt(idx2--));
}
carry = temp > 9 ? 1 : 0;
temp = temp > 9 ? temp % 10 : temp;
sb.append(temp);
}
if (carry > 0) {
sb.append(carry);
}
return sb.reverse().toString();
}
}