File tree Expand file tree Collapse file tree 3 files changed +82
-0
lines changed
solution/0900-0999/0967.Numbers With Same Consecutive Differences Expand file tree Collapse file tree 3 files changed +82
-0
lines changed Original file line number Diff line number Diff line change @@ -178,6 +178,35 @@ func numsSameConsecDiff(n int, k int) []int {
178
178
}
179
179
```
180
180
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
+
181
210
<!-- tabs:end -->
182
211
183
212
<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -161,6 +161,35 @@ func numsSameConsecDiff(n int, k int) []int {
161
161
}
162
162
```
163
163
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
+
164
193
<!-- tabs:end -->
165
194
166
195
<!-- solution:end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments