forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_66.java
44 lines (41 loc) · 1.15 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
package com.fishercoder.solutions;
/**
* 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 {
public static class Solution1 {
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;
}
}
}