forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlus One.java
More file actions
35 lines (28 loc) · 804 Bytes
/
Plus One.java
File metadata and controls
35 lines (28 loc) · 804 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
class Solution {
public int[] plusOne(int[] digits) {
int carry = 1;
List<Integer> ans = new ArrayList<>();
for (int i=digits.length-1;i>=0;i--) {
if (digits[i] == 9 && carry == 1) {
ans.add(0);
carry = 1;
}
else if (carry == 1) {
ans.add(digits[i]+carry);
carry = 0;
}
else {
ans.add(digits[i]+carry);
}
}
if (carry!=0) {
ans.add(carry);
}
Collections.reverse(ans);
int [] a = new int[ans.size()];
for (int k=0; k<ans.size();k++) {
a[k] = ans.get(k);
}
return a;
}
}