-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddToArrayForm.java
More file actions
28 lines (25 loc) · 828 Bytes
/
Copy pathAddToArrayForm.java
File metadata and controls
28 lines (25 loc) · 828 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
package LeetcodePractice;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class AddToArrayForm {
public static List<Integer> addToArrayForm(int[] num, int k) {
List<Integer> result = new ArrayList<>();
int i = num.length - 1, carry = k;
while (i >= 0 || carry > 0) {
if (i >= 0) carry += num[i--];
result.add(carry % 10);
carry /= 10;
}
Collections.reverse(result);
return result;
}
public static void main(String[] args) {
int[] num1 = {1,2,0,0};
System.out.println(addToArrayForm(num1, 34));
int[] num2 = {2,7,4};
System.out.println(addToArrayForm(num2, 181));
int[] num3 = {2,1,5};
System.out.println(addToArrayForm(num3, 806));
}
}