File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1291. Sequential Digits
3
+ * https://leetcode.com/problems/sequential-digits/
4
+ * Difficulty: Medium
5
+ *
6
+ * An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
7
+ * Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.
8
+ */
9
+
10
+ /**
11
+ * @param {number } low
12
+ * @param {number } high
13
+ * @return {number[] }
14
+ */
15
+ var sequentialDigits = function ( low , high ) {
16
+ return getSequentialNumbers ( ) . filter ( n => n >= low && n <= high ) ;
17
+ } ;
18
+
19
+ function getSequentialNumbers ( ) {
20
+ const numbers = new Set ( [ ] ) ;
21
+
22
+ for ( let i = 0 ; i < 10 ; i ++ ) {
23
+ for ( let j = 9 ; j > 0 && j > i + 1 ; j -- ) {
24
+ numbers . add ( + '123456789' . slice ( i , j ) ) ;
25
+ }
26
+ }
27
+
28
+ return [ ...numbers ] . sort ( ( a , b ) => a - b ) ;
29
+ }
You can’t perform that action at this time.
0 commit comments