Skip to content

Commit ea133c3

Browse files
authoredJul 10, 2024··
feat: add js/ts solution to lc problem: No.0967 (#3243)
1 parent 67e2ec0 commit ea133c3

File tree

4 files changed

+164
-0
lines changed

4 files changed

+164
-0
lines changed
 

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

+58
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,64 @@ func numsSameConsecDiff(n int, k int) []int {
178178
}
179179
```
180180

181+
#### TypeScript
182+
183+
```ts
184+
function numsSameConsecDiff(n: number, k: number): number[] {
185+
const ans = new Set<number>();
186+
const boundary = 10 ** (n - 1);
187+
188+
const dfs = (nums: number) => {
189+
if (nums >= boundary) {
190+
ans.add(nums);
191+
return;
192+
}
193+
194+
const num = nums % 10;
195+
for (const x of [num + k, num - k]) {
196+
if (0 <= x && x < 10) {
197+
dfs(nums * 10 + x);
198+
}
199+
}
200+
};
201+
202+
for (let i = 1; i < 10; i++) {
203+
dfs(i);
204+
}
205+
206+
return [...ans];
207+
}
208+
```
209+
210+
#### JavaScript
211+
212+
```js
213+
function numsSameConsecDiff(n, k) {
214+
const ans = new Set();
215+
const boundary = 10 ** (n - 1);
216+
217+
const dfs = nums => {
218+
if (nums >= boundary) {
219+
ans.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 [...ans];
236+
}
237+
```
238+
181239
<!-- tabs:end -->
182240

183241
<!-- solution:end -->

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

+58
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,64 @@ func numsSameConsecDiff(n int, k int) []int {
161161
}
162162
```
163163

164+
#### TypeScript
165+
166+
```ts
167+
function numsSameConsecDiff(n: number, k: number): number[] {
168+
const ans = new Set<number>();
169+
const boundary = 10 ** (n - 1);
170+
171+
const dfs = (nums: number) => {
172+
if (nums >= boundary) {
173+
ans.add(nums);
174+
return;
175+
}
176+
177+
const num = nums % 10;
178+
for (const x of [num + k, num - k]) {
179+
if (0 <= x && x < 10) {
180+
dfs(nums * 10 + x);
181+
}
182+
}
183+
};
184+
185+
for (let i = 1; i < 10; i++) {
186+
dfs(i);
187+
}
188+
189+
return [...ans];
190+
}
191+
```
192+
193+
#### JavaScript
194+
195+
```js
196+
function numsSameConsecDiff(n, k) {
197+
const ans = new Set();
198+
const boundary = 10 ** (n - 1);
199+
200+
const dfs = nums => {
201+
if (nums >= boundary) {
202+
ans.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 [...ans];
219+
}
220+
```
221+
164222
<!-- tabs:end -->
165223

166224
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function numsSameConsecDiff(n, k) {
2+
const ans = new Set();
3+
const boundary = 10 ** (n - 1);
4+
5+
const dfs = nums => {
6+
if (nums >= boundary) {
7+
ans.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 [...ans];
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function numsSameConsecDiff(n: number, k: number): number[] {
2+
const ans = new Set<number>();
3+
const boundary = 10 ** (n - 1);
4+
5+
const dfs = (nums: number) => {
6+
if (nums >= boundary) {
7+
ans.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 [...ans];
24+
}

0 commit comments

Comments
 (0)
Please sign in to comment.