-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path66.java
More file actions
29 lines (25 loc) · 710 Bytes
/
Copy path66.java
File metadata and controls
29 lines (25 loc) · 710 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
import java.util.Arrays;
class Solution {
public int[] plusOne(int[] digits) {
boolean carry = true;
int[] result;
for (int i = digits.length - 1;i >= 0;i--) {
if (carry) {
digits[i]++;
}
carry = ((digits[i] / 10) == 1 ? true : false);
digits[i] %= 10;
}
if (carry) {
result = new int[digits.length + 1];
result[0] = 1;
System.arraycopy(digits, 0, result, 1, digits.length);
} else {
result = digits;
}
return result;
}
public static void main(String[] args) {
new Solution().plusOne(new int[]{9 , 9 ,9});
}
}