-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlusOne_66.java
More file actions
44 lines (40 loc) · 1.16 KB
/
Copy pathPlusOne_66.java
File metadata and controls
44 lines (40 loc) · 1.16 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
package Array;
import java.util.Arrays;
/**
* 将数组表示的数加1,数组长度为1到100,考虑进位问题,注意下第一位是否有进位即可
*/
public class PlusOne_66 {
public static int[] plusOne(int[] digits) {
int carry=0;
int[] num=new int[digits.length+1];
int fromIndex=digits.length-1,toIndex=digits.length;
for(;fromIndex>=0;fromIndex--){
int no=0;
if(fromIndex==digits.length-1) {
no=digits[fromIndex]+1+carry;
}else {
no=digits[fromIndex]+carry;
}
if(no>9){
carry=1;
num[toIndex--]=no%10;
}else{
carry=0;
num[toIndex--]=no;
}
}
if(carry==1) {
num[0]=1;
return Arrays.copyOfRange(num,0,num.length);
}else {
return Arrays.copyOfRange(num,1,num.length);
}
}
public static void main(String[] args) {
int[] digits={1,2,4};
int[] ints = PlusOne_66.plusOne(digits);
for (int i:ints) {
System.out.println(i);
}
}
}