File tree 2 files changed +44
-1
lines changed
2 files changed +44
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,051 LeetCode solutions in JavaScript
1
+ # 1,052 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcode.com/ ] ( https://leetcode.com/ )
4
4
775
775
964|[ Least Operators to Express Number] ( ./solutions/0964-least-operators-to-express-number.js ) |Hard|
776
776
965|[ Univalued Binary Tree] ( ./solutions/0965-univalued-binary-tree.js ) |Easy|
777
777
966|[ Vowel Spellchecker] ( ./solutions/0966-vowel-spellchecker.js ) |Medium|
778
+ 967|[ Numbers With Same Consecutive Differences] ( ./solutions/0967-numbers-with-same-consecutive-differences.js ) |Medium|
778
779
970|[ Powerful Integers] ( ./solutions/0970-powerful-integers.js ) |Easy|
779
780
976|[ Largest Perimeter Triangle] ( ./solutions/0976-largest-perimeter-triangle.js ) |Easy|
780
781
977|[ Squares of a Sorted Array] ( ./solutions/0977-squares-of-a-sorted-array.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 967. Numbers With Same Consecutive Differences
3
+ * https://leetcode.com/problems/numbers-with-same-consecutive-differences/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given two integers n and k, return an array of all the integers of length n where the difference
7
+ * between every two consecutive digits is k. You may return the answer in any order.
8
+ *
9
+ * Note that the integers should not have leading zeros. Integers as 02 and 043 are not allowed.
10
+ */
11
+
12
+ /**
13
+ * @param {number } n
14
+ * @param {number } k
15
+ * @return {number[] }
16
+ */
17
+ var numsSameConsecDiff = function ( n , k ) {
18
+ const result = [ ] ;
19
+
20
+ for ( let digit = 1 ; digit <= 9 ; digit ++ ) {
21
+ buildNumber ( digit , n - 1 ) ;
22
+ }
23
+
24
+ return result ;
25
+
26
+ function buildNumber ( current , digitsLeft ) {
27
+ if ( digitsLeft === 0 ) {
28
+ result . push ( current ) ;
29
+ return ;
30
+ }
31
+
32
+ const lastDigit = current % 10 ;
33
+
34
+ if ( lastDigit + k <= 9 ) {
35
+ buildNumber ( current * 10 + lastDigit + k , digitsLeft - 1 ) ;
36
+ }
37
+
38
+ if ( k !== 0 && lastDigit - k >= 0 ) {
39
+ buildNumber ( current * 10 + lastDigit - k , digitsLeft - 1 ) ;
40
+ }
41
+ }
42
+ } ;
You can’t perform that action at this time.
0 commit comments