Skip to content

Commit 88241ee

Browse files
committed
#66 plus one
1 parent b990734 commit 88241ee

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Array.Lib
7+
{
8+
// Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
9+
10+
//You may assume the integer do not contain any leading zero, except the number 0 itself.
11+
12+
//The digits are stored such that the most significant digit is at the head of the list.
13+
public class PlusOneSln
14+
{
15+
public int[] PlusOne(int[] digits)
16+
{
17+
int index = digits.Length - 1;
18+
if(digits[index]<9)
19+
{
20+
digits[index]++;
21+
return digits;
22+
}
23+
if(index==0) return new int[]{1,0};
24+
int i = index;
25+
while(digits[i]==9)
26+
{
27+
digits[i] = 0; //位溢出
28+
if(i==0) //所有的位溢出
29+
{
30+
int [] rtn = new int[index+2];
31+
rtn[0]=1;
32+
return rtn;
33+
}
34+
i--;
35+
}
36+
digits[i]++; //第i位不为9(i > 0)
37+
return digits;
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)