-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plus One.java
37 lines (35 loc) · 935 Bytes
/
Plus One.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
package leetcodeOJ;
public class plusOne {
public static int[] plusOne(int[] digits) {
int len = digits.length;
int carry=1;
boolean flag = false;
for(int i=len-1;i>=0;i--){
digits[i] = digits[i]+carry;
if(digits[i]==10){
digits[i] = digits[i]%10;
carry = 1;
if(i==0){
flag = true;
}
}else{
carry=0;
}
}
if(flag==false){
return digits;
}else{
int[] sum = new int[len+1];
sum[0]=1;
for(int j=1;j<len+1;j++){
sum[j] = digits[j-1];
}
return sum;
}
}
public static void main(String[] argv){
int[] digits = {9,8,7,6,5,4,3,2,1,0};
plusOne(digits);
System.out.println("finish");
}
}