-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplus1.java
More file actions
34 lines (24 loc) · 862 Bytes
/
plus1.java
File metadata and controls
34 lines (24 loc) · 862 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
import java.util.*;
class Solution {
public int[] plusOne(int[] digits) {
ArrayList<Integer> arrayList = new ArrayList<>();
int lastIdx = digits.length - 1;
int carry = 1;
for(int i=lastIdx;i>=0;i--){
int sum = digits[i] + carry;
carry = sum /10;
arrayList.add(0,sum % 10);
}
if(carry>0){
arrayList.add(0,carry);
}
// Convert ArrayList to an Integer[] array
Integer[] integerArray = arrayList.toArray(new Integer[0]);
// Convert Integer[] array to an int[] array
int[] resultArray = new int[integerArray.length];
for (int i = 0; i < integerArray.length; i++) {
resultArray[i] = integerArray[i];
}
return resultArray;
}
}