Skip to content

Commit 53a5323

Browse files
authored
Create 1291-sequential-digits.js
1 parent df36e50 commit 53a5323

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

1291-sequential-digits.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @param {number} low
3+
* @param {number} high
4+
* @return {number[]}
5+
*/
6+
const sequentialDigits = function(low, high) {
7+
const set = new Set()
8+
let start = 0, end = 0
9+
for(let i = 10; i >= 0; i--) {
10+
if (low / (10 ** i) >= 1) {
11+
start = ~~(low / (10 ** i))
12+
break
13+
}
14+
}
15+
for(let i = 10; i >= 0; i--) {
16+
if (high / (10 ** i) >= 1) {
17+
end = ~~(high / (10 ** i))
18+
break
19+
}
20+
}
21+
for(let i = 1; i <= 9; i++) {
22+
helper(`${i}`)
23+
}
24+
25+
const res = Array.from(set)
26+
res.sort((a, b) => a- b)
27+
return res
28+
29+
function helper(s) {
30+
// console.log(s)
31+
if(+s > high) return
32+
if(+s >= low && +s <= high) {
33+
set.add(+s)
34+
}
35+
if(s[s.length - 1] === '9') return
36+
helper(`${s}${+s[s.length - 1] + 1}`)
37+
}
38+
};

0 commit comments

Comments
 (0)