forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_989.java
45 lines (43 loc) · 1.38 KB
/
_989.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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class _989 {
public static class Solution1 {
public List<Integer> addToArrayForm(int[] A, int K) {
List<Integer> kDigitsReversed = new ArrayList<>();
int divisor = 10;
while (K != 0) {
kDigitsReversed.add(K % divisor);
K /= 10;
}
List<Integer> result = new ArrayList<>();
int prevFlow = 0;
for (int i = A.length - 1, j = 0; i >= 0 || j < kDigitsReversed.size(); i--, j++) {
int sum;
if (i >= 0 && j < kDigitsReversed.size()) {
sum = A[i] + kDigitsReversed.get(j);
} else if (i >= 0) {
sum = A[i];
} else {
sum = kDigitsReversed.get(j);
}
int flow = 0;
if (prevFlow != 0) {
sum += prevFlow;
}
if (sum > 9) {
flow = 1;
}
sum %= 10;
prevFlow = flow;
result.add(sum);
}
if (prevFlow != 0) {
result.add(prevFlow);
}
Collections.reverse(result);
return result;
}
}
}