Skip to content

Commit 36b7a78

Browse files
committed
feat: add ts solution to lc problem: No.0967x
1 parent b8cc919 commit 36b7a78

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
@@ -178,6 +178,35 @@ 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 res = new Set<number>();
186+
const boundary = 10 ** (n - 1);
187+
188+
const dfs = (nums: number) => {
189+
if (nums >= boundary) {
190+
res.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 [...res];
207+
}
208+
```
209+
181210
<!-- tabs:end -->
182211

183212
<!-- 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
@@ -161,6 +161,35 @@ 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 res = new Set<number>();
169+
const boundary = 10 ** (n - 1);
170+
171+
const dfs = (nums: number) => {
172+
if (nums >= boundary) {
173+
res.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 [...res];
190+
}
191+
```
192+
164193
<!-- tabs:end -->
165194

166195
<!-- solution:end -->
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function numsSameConsecDiff(n: number, k: number): number[] {
2+
const res = new Set<number>();
3+
const boundary = 10 ** (n - 1);
4+
5+
const dfs = (nums: number) => {
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)