-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd2ArrayFormOfInt.java
More file actions
50 lines (44 loc) · 1.43 KB
/
Add2ArrayFormOfInt.java
File metadata and controls
50 lines (44 loc) · 1.43 KB
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
package leetcode;
import java.util.LinkedList;
import java.util.List;
/**
* Add2ArrayFormOfInt
* https://leetcode-cn.com/problems/add-to-array-form-of-integer/
* 989. 数组形式的整数加法
* https://leetcode-cn.com/problems/add-to-array-form-of-integer/solution/mo-ni-da-shu-cao-zuo-by-oshdyr-zml5/
*
* @since 2021-01-22
*/
public class Add2ArrayFormOfInt {
public static void main(String[] args) {
Add2ArrayFormOfInt sol = new Add2ArrayFormOfInt();
// List<Integer> res = sol.addToArrayForm(new int[]{9, 9, 8, 9}, 33);
List<Integer> res = sol.addToArrayForm(new int[]{2, 1, 5}, 806);
// List<Integer> res = sol.addToArrayForm(new int[]{}, 806);
for (Integer value : res) {
System.out.print(value + ", ");
}
System.out.println();
}
public List<Integer> addToArrayForm(int[] A, int K) {
List<Integer> result = new LinkedList<>();
for (int v : A) {
result.add(v);
}
int left = K;
int inc = 0;
for (int idx = result.size() - 1; idx >= 0; idx--) {
int sum = left % 10 + inc + result.get(idx);
left /= 10;
result.set(idx, sum % 10);
inc = sum / 10;
}
while (left > 0 || inc > 0) {
int sum = left % 10 + inc;
left /= 10;
result.add(0, sum % 10);
inc = sum / 10;
}
return result;
}
}