Skip to content

Commit 707d220

Browse files
committed
feat: add js solution to lc problem: No.0967
1 parent 36b7a78 commit 707d220

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

solution/0900-0999/0967.Numbers With Same Consecutive Differences/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,35 @@ function numsSameConsecDiff(n: number, k: number): number[] {
207207
}
208208
```
209209

210+
#### JavaScript
211+
212+
```ts
213+
function numsSameConsecDiff(n, k) {
214+
const res = new Set();
215+
const boundary = 10 ** (n - 1);
216+
217+
const dfs = nums => {
218+
if (nums >= boundary) {
219+
res.add(nums);
220+
return;
221+
}
222+
223+
const num = nums % 10;
224+
for (const x of [num + k, num - k]) {
225+
if (0 <= x && x < 10) {
226+
dfs(nums * 10 + x);
227+
}
228+
}
229+
};
230+
231+
for (let i = 1; i < 10; i++) {
232+
dfs(i);
233+
}
234+
235+
return [...res];
236+
}
237+
```
238+
210239
<!-- tabs:end -->
211240

212241
<!-- solution:end -->

solution/0900-0999/0967.Numbers With Same Consecutive Differences/README_EN.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,35 @@ function numsSameConsecDiff(n: number, k: number): number[] {
190190
}
191191
```
192192

193+
#### JavaScript
194+
195+
```js
196+
function numsSameConsecDiff(n: number, k: number): number[] {
197+
const res = new Set<number>();
198+
const boundary = 10 ** (n - 1);
199+
200+
const dfs = (nums: number) => {
201+
if (nums >= boundary) {
202+
res.add(nums);
203+
return;
204+
}
205+
206+
const num = nums % 10;
207+
for (const x of [num + k, num - k]) {
208+
if (0 <= x && x < 10) {
209+
dfs(nums * 10 + x);
210+
}
211+
}
212+
};
213+
214+
for (let i = 1; i < 10; i++) {
215+
dfs(i);
216+
}
217+
218+
return [...res];
219+
}
220+
```
221+
193222
<!-- tabs:end -->
194223

195224
<!-- solution:end -->
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function numsSameConsecDiff(n, k) {
2+
const res = new Set();
3+
const boundary = 10 ** (n - 1);
4+
5+
const dfs = nums => {
6+
if (nums >= boundary) {
7+
res.add(nums);
8+
return;
9+
}
10+
11+
const num = nums % 10;
12+
for (const x of [num + k, num - k]) {
13+
if (0 <= x && x < 10) {
14+
dfs(nums * 10 + x);
15+
}
16+
}
17+
};
18+
19+
for (let i = 1; i < 10; i++) {
20+
dfs(i);
21+
}
22+
23+
return [...res];
24+
}

0 commit comments

Comments
 (0)