-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddToArray.java
More file actions
36 lines (29 loc) · 794 Bytes
/
AddToArray.java
File metadata and controls
36 lines (29 loc) · 794 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
31
32
33
34
35
36
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
class AddToArray {
public List <Integer> AddToArray(int []num, int k){
List<Integer> ans = new ArrayList<>();
int p = num.length-1;
int carry = 0;
while (p>=0 || k>0) {
int numvalu = 0;
if(p>=0){
numvalu = num[p];
}
int d = k % 10;
int sum = numvalu + d + carry;
int digit = sum % 10;
carry = sum/10;
ans.add(digit);
p--;
k = k/10;
}
if(carry > 0){
ans.add(carry);
}
Collections.reverse(ans);
return ans;
}
}