-
Notifications
You must be signed in to change notification settings - Fork 91
/
PlusOne66.java
43 lines (39 loc) · 1.12 KB
/
PlusOne66.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
/**
* Given a non-negative integer represented as a non-empty array of digits,
* plus one to the integer.
*
* You may assume the integer do not contain any leading zero, except the
* number 0 itself.
*
* The digits are stored such that the most significant digit is at the head
* of the list.
*/
public class PlusOne66 {
public int[] plusOne(int[] digits) {
int[] res = new int[digits.length+1];
int c = 1;
for (int i=digits.length-1; i>=0; i--) {
int s = digits[i] + c;
c = s / 10;
res[i+1] = s % 10;
}
res[0] = c;
return (res[0] != 0) ? res : Arrays.copyOfRange(res, 1, res.length);
}
/**
* https://leetcode.com/problems/plus-one/discuss/24082/My-Simple-Java-Solution
*/
public int[] plusOne2(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[] newNumber = new int [n+1];
newNumber[0] = 1;
return newNumber;
}
}