-
Notifications
You must be signed in to change notification settings - Fork 1
/
AddToArrayFormOfInteger.java
60 lines (57 loc) · 1.5 KB
/
AddToArrayFormOfInteger.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
53
54
55
56
57
58
59
60
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* 989. Add to Array-Form of Integer
* @author LBW
*/
public class AddToArrayFormOfInteger {
public List<Integer> addToArrayForm(int[] A, int K) {
int[] B = intToArray(K);
return arrayAddArray(A, B);
}
private int[] intToArray(int k) {
List<Integer> res = new LinkedList<>();
while (k != 0) {
res.add(0, k % 10);
k = k / 10;
}
int[] ans = new int[res.size()];
for (int i = 0; i < res.size(); i++) {
ans[i] = res.get(i);
}
return ans;
}
private List<Integer> arrayAddArray(int[] A, int[] B) {
int n1 = A.length, n2 = B.length;
List<Integer> res = new LinkedList<>();
int i = A.length - 1, j = B.length - 1, carry = 0;
while (i >= 0 && j >= 0) {
int a = A[i], b = B[j];
int t = a + b + carry;
carry = t >= 10 ? 1 : 0;
t = t % 10;
res.add(0, t);
i--;
j--;
}
while (i >= 0) {
int t = A[i] + carry;
carry = t >= 10 ? 1 : 0;
t = t % 10;
res.add(0, t);
i--;
}
while (j >= 0) {
int t = B[j] + carry;
carry = t >= 10 ? 1 : 0;
t = t % 10;
res.add(0, t);
j--;
}
if (carry == 1) {
res.add(0, 1);
}
return res;
}
}