Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfe96f42

Browse files
committed
plus
1 parent23c51b1 commitfe96f42

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

‎array/PlusOne.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
packageAlgorithms.array;
2+
3+
publicclassPlusOne {
4+
publicint[]plusOne(int[]digits) {
5+
if (digits ==null) {
6+
returnnull;
7+
}
8+
9+
intoverFlow =0;
10+
11+
intlen =digits.length;
12+
int[]ret =newint[len];
13+
14+
for (inti =len -1;i >=0;i--) {
15+
intsum =digits[i] +overFlow;
16+
if (i ==len -1) {
17+
// 只有最后一位需要加1
18+
sum++;
19+
}
20+
21+
// 溢出的话,置溢出位。
22+
if (sum >9) {
23+
overFlow =1;
24+
}else {
25+
overFlow =0;
26+
}
27+
28+
// 把高位去掉,因为我们要0-9
29+
ret[i] =sum %10;
30+
}
31+
32+
if (overFlow ==1) {
33+
int[]retOver =newint[len +1];
34+
System.arraycopy(retOver,1,ret,0,len);
35+
retOver[0] =1;
36+
returnretOver;
37+
}
38+
39+
returnret;
40+
}
41+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp