-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlusOne.java
More file actions
32 lines (28 loc) · 635 Bytes
/
Copy pathPlusOne.java
File metadata and controls
32 lines (28 loc) · 635 Bytes
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
package Array;
public class PlusOne
{
public int[] plusOne(int[] digits)
{
boolean overflow = false;
int length = digits.length;
for (int i = length-1; i >= 0; i--)
{
if (digits[i]+1 == 10)
{
overflow = true;
digits[i] = 0;
}else {
digits[i] = digits[i] + 1;
return digits;
}
}
if (overflow)
{
int newdigits[] = new int[length + 1];
System.arraycopy(digits, 0, newdigits, 1, length);//java复制数组的方法System.arraycopy(当位置跟origin不一样的时候,如果单纯的复制,用Arrays.copyOf())
newdigits[0] = 1;
return newdigits;
}
return digits;
}
}