Skip to content

Commit 261d779

Browse files
committed
Add solution #66
1 parent a361a70 commit 261d779

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
36|[Valid Sudoku](./0036-valid-sudoku.js)|Medium|
1111
43|[Multiply Strings](./0043-multiply-strings.js)|Medium|
1212
58|[Length of Last Word](./0058-length-of-last-word.js)|Easy|
13+
66|[Plus One](./0066-plus-one.js)|Easy|
1314
67|[Add Binary](./0067-add-binary.js)|Easy|
1415
151|[Reverse Words in a String](./0151-reverse-words-in-a-string.js)|Medium|
1516
226|[Invert Binary Tree](./0226-invert-binary-tree.js)|Easy|

solutions/0066-plus-one.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 66. Plus One
3+
* https://leetcode.com/problems/plus-one/
4+
* Difficulty: Easy
5+
*
6+
* Given a non-empty array of digits representing a non-negative integer,
7+
* plus one to the integer.
8+
*
9+
* The digits are stored such that the most significant digit is at the
10+
* head of the list, and each element in the array contain a single digit.
11+
*
12+
* You may assume the integer does not contain any leading zero,
13+
* except the number 0 itself.
14+
*/
15+
16+
/**
17+
* @param {number[]} digits
18+
* @return {number[]}
19+
*/
20+
var plusOne = function(digits) {
21+
let i = digits.length - 1;
22+
digits[i] += 1;
23+
while (digits[i] > 9) {
24+
digits[i--] = 0;
25+
if (digits[i]) {
26+
digits[i] += 1;
27+
} else {
28+
digits.unshift(1);
29+
}
30+
}
31+
return digits;
32+
};
33+
34+
// alternative one-liner that breaks the rules:
35+
/**
36+
* @param {number[]} digits
37+
* @return {number[]}
38+
*/
39+
var plusOne = function(digits) {
40+
return String(BigInt(digits.join('')) + BigInt(1)).split('');
41+
};

0 commit comments

Comments
 (0)