forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_66.java
33 lines (30 loc) · 907 Bytes
/
_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
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) {
int len = digits.length;
int[] temp = digits;
for (int i = len - 1; i >= 0; i--) {
if (temp[i] + 1 == 10) {
temp[i] = 0;
} else {
temp[i] += 1;
return temp;
}
}
if (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;
} else {
return temp;
}
}
}
}