-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path66加一.java
41 lines (39 loc) · 994 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
34
35
36
37
38
39
40
41
import java.util.*;
public class test {
public static void main(String[] args){
Solution s = new Solution();
int[] test = {9,9,9};
int[] result = s.plusOne(test);
for(int i=0;i<result.length;i++){
System.out.print(result[i]+" ");
}
System.out.println();
}
}
class Solution {
public int[] plusOne(int[] digits) {
int temp = 1;
for(int i=digits.length-1;i>=0;i--){
digits[i] += temp;
if(digits[i]>=10){
digits[i] = digits[i] % 10;
temp = 1;
}else{
temp = 0;
}
}
if(temp == 0){
return digits;
}
return insert(digits);
}
public int[] insert(int[] digits){
int length = digits.length;
int[] temp = new int[length+1];
temp[0] = 1;
for(int i=1;i<=length;i++){
temp[i] = digits[i-1];
}
return temp;
}
}