-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlusOne.java
More file actions
31 lines (28 loc) · 752 Bytes
/
PlusOne.java
File metadata and controls
31 lines (28 loc) · 752 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
package com.leetcode;
import java.util.Arrays;
/**
* Author: yhl
* DateTime: 2019/11/7 7:49
* Description: write some description
*/
public class PlusOne {
public static void main(String[] args) {
int[] arr = {9, 9, 9};
PlusOne plusOne = new PlusOne();
int[] newArr = plusOne.plusOne(arr);
System.out.println(Arrays.toString(newArr));
}
public int[] plusOne(int[] digits) {
int length = digits.length - 1;
while (length >= 0) {
if (digits[length] < 9) {
digits[length]++;
return digits;
}
digits[length--] = 0;
}
int[] arr = new int[digits.length + 1];
arr[0] = 1;
return arr;
}
}