-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddStrings.java
More file actions
30 lines (22 loc) · 830 Bytes
/
Copy pathAddStrings.java
File metadata and controls
30 lines (22 loc) · 830 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
28
29
30
// Given two non-negative integers num1 and num2 represented as string,
// return the sum of num1 and num2.
// See: https://leetcode.com/problems/add-strings/
package leetcode.string;
public class AddStrings {
public String addStrings(String num1, String num2) {
StringBuilder res = new StringBuilder();
int l1 = num1.length() - 1, l2 = num2.length() - 1;
int carry = 0;
while (l1 >= 0 || l2 >= 0) {
int n1 = l1 >= 0 ? num1.charAt(l1--) - 48 : 0;
int n2 = l2 >= 0 ? num2.charAt(l2--) - 48 : 0;
res.append((n1 + n2 + carry) % 10);
carry = (n1 + n2 + carry) / 10;
}
if (carry > 0)
res.append(carry);
return res.reverse().toString();
}
public static void main(String[] args) {
}
}