-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlusOne.java
More file actions
57 lines (49 loc) · 1.06 KB
/
PlusOne.java
File metadata and controls
57 lines (49 loc) · 1.06 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package collection3;
/*
* 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 PlusOne {
public static int[] plusOne(int[] digits) {
int flag = 1;
for(int i = digits.length-1; i>=0;i--){
if(flag ==0){
return digits;
}
if(i==0&&digits[i]==0){
digits[i]=1;
return digits;
}
if(i==0 &&digits[i]==1){
int[] digit = new int[digits.length+1];
digit[0]=1;
return digit;
}
if(digits[i]==0){
flag = 0;
digits[i] = 1;
} else if(digits[i] ==1){
flag = 1;
digits[i] = 0;
}
}
return null;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a1 = null;
int[] a2 = {1,1,1};
int[] b1 = plusOne(a1);
int[] b2 = plusOne(a2);
for(int i = 0 ;i<b1.length;i++){
System.out.print(b1[i]);
}
System.out.println();
for(int i = 0 ;i<b2.length;i++){
System.out.print(b2[i]);
}
System.out.println();
}
}