@@ -10,35 +10,24 @@ public class _66 {
10
10
11
11
public static class Solution1 {
12
12
public int []plusOne (int []digits ) {
13
- boolean carry =false ;
14
- int len =digits .length ;
15
- int []temp =digits ;
16
- //process the last digit at first, to get carry value
17
- if (digits [len -1 ] +1 ==10 ) {
18
- carry =true ;
19
- temp [len -1 ] =0 ;
20
- }else {
21
- temp [len -1 ] +=1 ;
22
- return temp ;
23
- }
24
-
25
- //start from the second last element
26
- for (int i =len -2 ;i >=0 ;i --) {
27
- if (carry &&temp [i ] +1 ==10 ) {
28
- temp [i ] =0 ;
29
- carry =true ;
30
- }else if (carry ) {
31
- temp [i ] +=1 ;
32
- carry =false ;
13
+ int len =digits .length ;
14
+ int []temp =digits ;
15
+
16
+ for (int i =len -1 ;i >=0 ;i --) {
17
+ if (temp [i ] +1 ==10 ) {
18
+ temp [i ] =0 ;
19
+ }else {
20
+ temp [i ] +=1 ;
21
+ return temp ;
22
+ }
23
+ }
24
+ if (temp [0 ] ==0 ) {
25
+ int []res =new int [len +1 ];
26
+ res [0 ] =1 ;//all the rest of the numbers should all be zeroes, so we don't need to copy from the original array
27
+ return res ;
28
+ }else {
29
+ return temp ;
33
30
}
34
- }
35
- if (carry &&temp [0 ] ==0 ) {
36
- int []res =new int [len +1 ];
37
- res [0 ] =1 ;
38
- //all the rest of the numbers should all be zeroes, so we don't need to copy from the original array
39
- return res ;
40
- }
41
- return temp ;
42
31
}
43
32
}
44
33
}