Skip to content

Commit 618f758

Browse files
committedMar 29, 2025
Add solution #967
1 parent 19e1b09 commit 618f758

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,051 LeetCode solutions in JavaScript
1+
# 1,052 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -775,6 +775,7 @@
775775
964|[Least Operators to Express Number](./solutions/0964-least-operators-to-express-number.js)|Hard|
776776
965|[Univalued Binary Tree](./solutions/0965-univalued-binary-tree.js)|Easy|
777777
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|
778779
970|[Powerful Integers](./solutions/0970-powerful-integers.js)|Easy|
779780
976|[Largest Perimeter Triangle](./solutions/0976-largest-perimeter-triangle.js)|Easy|
780781
977|[Squares of a Sorted Array](./solutions/0977-squares-of-a-sorted-array.js)|Easy|
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
};

0 commit comments

Comments
 (0)
Please sign in to comment.