Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fac0911

Browse files
committedDec 18, 2019
Add solution #1291
1 parent 6f32bb8 commit fac0911

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
 

‎solutions/1291-sequential-digits.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}

0 commit comments

Comments
 (0)
Please sign in to comment.