-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddToArrayForm.java
More file actions
30 lines (24 loc) · 865 Bytes
/
Copy pathaddToArrayForm.java
File metadata and controls
30 lines (24 loc) · 865 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
29
30
/* The array-form of an integer num is an array representing its digits in left to right order.
For example, for num = 1321, the array form is [1,3,2,1].
Given num, the array-form of an integer, and an integer k, return the array-form of the integer num + k. */
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Solution {
public List<Integer> addToArrayForm(int[] num, int k) {
List<Integer> result = new ArrayList<>();
int i = num.length - 1;
int carry = k;
while (i >= 0 || carry > 0) {
if (i >= 0) {
carry += num[i];
i--;
}
result.add(carry % 10);
carry /= 10;
}
// The result is built in reverse order, so reverse it
Collections.reverse(result);
return result;
}
}