forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 8
/
_66.java
52 lines (46 loc) · 1.63 KB
/
_66.java
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.fishercoder.solutions;
import com.fishercoder.common.utils.CommonUtils;
/**66. Plus One
Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.*/
public class _66 {
//also looked at Discuss, basically the same idea as mine, just their code is more concise.
public int[] plusOne(int[] digits) {
boolean carry = false;
int len = digits.length;
int[] temp = digits;
//process the last digit at first, to get carry value
if (digits[len - 1] + 1 == 10) {
carry = true;
temp[len - 1] = 0;
} else {
temp[len - 1] += 1;
return temp;
}
//start from the second last element
for (int i = len - 2; i >= 0; i--) {
if (carry && temp[i] + 1 == 10) {
temp[i] = 0;
carry = true;
} else if (carry) {
temp[i] += 1;
carry = false;
}
}
if (carry && temp[0] == 0) {
int[] res = new int[len + 1];
res[0] = 1;
//all the rest of the numbers should all be zeroes, so we don't need to copy from the original array
return res;
}
return temp;
}
public static void main(String... strings) {
_66 test = new _66();
// int[] digits = new int[]{9,9,9,9};
// int[] digits = new int[]{8,9,9,9};
int[] digits = new int[]{2, 4, 9, 3, 9};
int[] res = test.plusOne(digits);
CommonUtils.printArray(res);
}
}