-
Notifications
You must be signed in to change notification settings - Fork 43
/
PlusOne.java
50 lines (42 loc) · 1.33 KB
/
PlusOne.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
package LeetCodeJava.Array;
// https://leetcode.com/problems/plus-one/
public class PlusOne {
// V0
// V1
// https://leetcode.com/problems/plus-one/editorial/
public int[] plusOne(int[] digits) {
int n = digits.length;
// move along the input array starting from the end
for (int idx = n - 1; idx >= 0; --idx) {
// set all the nines at the end of array to zeros
if (digits[idx] == 9) {
digits[idx] = 0;
}
// here we have the rightmost not-nine
else {
// increase this rightmost not-nine by 1
digits[idx]++;
// and the job is done
return digits;
}
}
// we're here because all the digits are nines
digits = new int[n + 1];
digits[0] = 1;
return digits;
}
// V2
// https://leetcode.com/problems/plus-one/solutions/2706861/java-fastest-0ms-runtime-easy-and-elegant-solution/
public int[] plusOne_2(int[] digits){
for (int i = digits.length - 1; i >= 0; i--) {
if (digits[i] < 9) {
digits[i]++;
return digits;
}
digits[i] = 0;
}
digits = new int[digits.length + 1];
digits[0] = 1;
return digits;
}
}