forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path66-Plus-One.java
More file actions
29 lines (26 loc) · 729 Bytes
/
66-Plus-One.java
File metadata and controls
29 lines (26 loc) · 729 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
class Solution {
public int[] plusOne(int[] digits) {
final int len = digits.length;
int[] newDigits = new int[len + 1];
int carry = 1;
int currSum = 0;
for (int i = len - 1; i >= 0; i--) {
currSum = digits[i] + carry;
if (currSum > 9) {
digits[i] = currSum % 10;
newDigits[i + 1] = digits[i];
carry = 1;
} else {
digits[i] = currSum;
newDigits[i + 1] = digits[i];
carry = 0;
break;
}
}
if (carry == 1) {
newDigits[0] = 1;
return newDigits;
}
return digits;
}
}