-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddString.java
52 lines (47 loc) · 1.58 KB
/
AddString.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class AddString {
public String addStrings(String num1, String num2) {
if (num1 == null && num2 == null) return null;
if (num1 == null || num2 == null) return num1 == num2 ? num2 : num1;
if (num1.length() == 0 && num2.length() == 0) return "0";
int i = 0;
while (i < num1.length() - 1) {
if (num1.charAt(num1.length() - 1 - i) == '.') {
break;
}
i++;
}
i = i == num1.length() - 1 ? 0 : i;
int j = 0;
while (j < num2.length() - 1) {
if (num2.charAt(num2.length() - 1 - j) == '.') {
break;
}
j++;
}
j = j == num2.length() - 1 ? 0 : j;
String shorter = i < j ? num1 : num2;
String longer = i >= j ? num1 : num2;
for (int k = 0; k < Math.abs (i - j); k++) {
shorter += "0";
}
StringBuilder sb = new StringBuilder();
int one = shorter.length() - 1;
int two = longer.length() - 1;
int carry = 0;
while(one >= 0 || two >= 0) {
if (one >= 0 && shorter.charAt(one) == '.') {
sb.append('.');
} else {
int n1 = one >= 0 ? shorter.charAt(one) - '0' : 0;
int n2 = two >= 0 ? longer.charAt(two) - '0' : 0;
int tmp = n1 + n2 + carry;
carry = tmp / 10;
sb.append(tmp % 10);
}
one--;
two--;
}
if (carry == 1) sb.append(1);
return sb.reverse().toString();
}
}