-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlusOne.java
More file actions
37 lines (25 loc) · 828 Bytes
/
Copy pathPlusOne.java
File metadata and controls
37 lines (25 loc) · 828 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
36
37
package LeetcodePractice;
import java.util.Arrays;
public class PlusOne {
public static int[] plusOne(int[] digits) {
int n = digits.length;
for (int i = n - 1; i >= 0; i--) {
if (digits[i] < 9) {
digits[i]++;
return digits;
}
digits[i] = 0;
}
int[] result = new int[n + 1];
result[0] = 1;
return result;
}
public static void main(String[] args) {
int[] digits1 = {1, 2, 3};
System.out.println(Arrays.toString(plusOne(digits1)));
int[] digits2 = {4, 3, 2, 1};
System.out.println(Arrays.toString(plusOne(digits2)));
int[] digits3 = {9};
System.out.println(Arrays.toString(plusOne(digits3)));
}
}