File tree 2 files changed +16
-1
lines changed
2 files changed +16
-1
lines changed Original file line number Diff line number Diff line change 377
377
| 69| [ Sqrt(x)] ( https://leetcode.com/problems/sqrtx/ ) | | Medium|
378
378
| 68| [ Text Justification] ( https://leetcode.com/problems/text-justification/ ) | | Hard|
379
379
| 67| [ Add Binary] ( https://leetcode.com/problems/add-binary/ ) | [ java] ( ./algorithms/addBinary/Solution.java ) , [ js] ( ./algorithms/addBinary/Solution.js ) | Easy|
380
- | 66| [ Plus One] ( https://leetcode.com/problems/plus-one/ ) | | Easy|
380
+ | 66| [ Plus One] ( https://leetcode.com/problems/plus-one/ ) | [ js ] ( ./algorithms/plusOne/solution.js ) | Easy|
381
381
| 65| [ Valid Number] ( https://leetcode.com/problems/valid-number/ ) | [ js] ( ./algorithms/validNumber/validNumber.js ) | Easy|
382
382
| 64| [ Minimum Path Sum] ( https://leetcode.com/problems/minimum-path-sum/ ) | | Medium|
383
383
| 63| [ Unique Paths II] ( https://leetcode.com/problems/unique-paths-ii/ ) | | Medium|
Original file line number Diff line number Diff line change
1
+ var plusOne = function ( digits ) {
2
+ let carry = true ; // 进位标志,初始化的时候末位是需要加1的
3
+ for ( let index = digits . length - 1 ; index >= 0 ; index -- ) {
4
+ digits [ index ] = digits [ index ] + ( carry ? 1 : 0 ) ;
5
+ if ( digits [ index ] > 9 ) {
6
+ // 溢出
7
+ digits [ index ] = 0 ;
8
+ carry = true ;
9
+ } else {
10
+ carry = false ;
11
+ }
12
+ }
13
+
14
+ return carry ? [ 1 , ...digits ] : digits ; // 最后如果依旧有进位标志,还需要处理一下
15
+ } ;
You can’t perform that action at this time.
0 commit comments