-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAddStrings.java
More file actions
28 lines (25 loc) · 832 Bytes
/
AddStrings.java
File metadata and controls
28 lines (25 loc) · 832 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
package com.dbc;
public class AddStrings {
public String addStrings(String num1, String num2) {
String res = "";
int remain = 0;
int left = num1.length() - 1, right = num2.length() - 1;
while (left >= 0 && right >= 0) {
int temp = (num1.charAt(left) - '0') + (num2.charAt(right) - '0') + remain;
remain = temp / 10;
res = "" + (temp % 10) + res;
left--;
right--;
}
int point = Math.max(left, right);
String num = left >= 0 ? num1 : num2;
while (point >= 0) {
int temp = (num.charAt(point) - '0') + remain;
remain = temp / 10;
res = "" + (temp % 10) + res;
point--;
}
if (remain != 0) res = "" + remain + res;
return res;
}
}