File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Array/Array.Console/Array.Lib Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments