Skip to content

Commit bd78fab

Browse files
committed
22w6: add 66 js solution
1 parent 224b15e commit bd78fab

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@
377377
|69|[Sqrt(x)](https://leetcode.com/problems/sqrtx/)| |Medium|
378378
|68|[Text Justification](https://leetcode.com/problems/text-justification/)| |Hard|
379379
|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|
381381
|65|[Valid Number](https://leetcode.com/problems/valid-number/)| [js](./algorithms/validNumber/validNumber.js) |Easy|
382382
|64|[Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/)| |Medium|
383383
|63|[Unique Paths II](https://leetcode.com/problems/unique-paths-ii/)| |Medium|

algorithms/plusOne/solution.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
};

0 commit comments

Comments
 (0)