File tree 4 files changed +164
-0
lines changed
solution/0900-0999/0967.Numbers With Same Consecutive Differences
4 files changed +164
-0
lines changed Original file line number Diff line number Diff line change @@ -178,6 +178,64 @@ 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 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
+
181
239
<!-- tabs:end -->
182
240
183
241
<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -161,6 +161,64 @@ 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 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
+
164
222
<!-- tabs:end -->
165
223
166
224
<!-- solution:end -->
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments