File tree 2 files changed +42
-0
lines changed
2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 10
10
36|[ Valid Sudoku] ( ./0036-valid-sudoku.js ) |Medium|
11
11
43|[ Multiply Strings] ( ./0043-multiply-strings.js ) |Medium|
12
12
58|[ Length of Last Word] ( ./0058-length-of-last-word.js ) |Easy|
13
+ 66|[ Plus One] ( ./0066-plus-one.js ) |Easy|
13
14
67|[ Add Binary] ( ./0067-add-binary.js ) |Easy|
14
15
151|[ Reverse Words in a String] ( ./0151-reverse-words-in-a-string.js ) |Medium|
15
16
226|[ Invert Binary Tree] ( ./0226-invert-binary-tree.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments