Skip to content

Commit 01b5c56

Browse files
committed
feat: add No.1291
1 parent 199efae commit 01b5c56

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

1201-1300/1291. Sequential Digits.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# 1291. Sequential Digits
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Enumeration.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
An integer has **sequential digits** if and only if each digit in the number is one more than the previous digit.
10+
11+
Return a **sorted** list of all the integers in the range `[low, high]` inclusive that have sequential digits.
12+
13+
 
14+
Example 1:
15+
```
16+
Input: low = 100, high = 300
17+
Output: [123,234]
18+
```Example 2:
19+
```
20+
Input: low = 1000, high = 13000
21+
Output: [1234,2345,3456,4567,5678,6789,12345]
22+
```
23+
 
24+
**Constraints:**
25+
26+
27+
28+
- `10 <= low <= high <= 10^9`
29+
30+
31+
32+
## Solution
33+
34+
```javascript
35+
/**
36+
* @param {number} low
37+
* @param {number} high
38+
* @return {number[]}
39+
*/
40+
var sequentialDigits = function(low, high) {
41+
var len1 = low.toString().length;
42+
var len2 = high.toString().length;
43+
var res = [];
44+
for (var i = len1; i <= len2; i++) {
45+
for (var j = 1; j <= 9 - i + 1; j++) {
46+
var num = Array(i).fill(0)
47+
.map((_, index) => j + index)
48+
.reduce((sum, n) => sum * 10 + n, 0);
49+
if (num >= low && num <= high) {
50+
res.push(num);
51+
}
52+
}
53+
}
54+
return res;
55+
};
56+
```
57+
58+
**Explain:**
59+
60+
nope.
61+
62+
**Complexity:**
63+
64+
* Time complexity : O(log(n)).
65+
* Space complexity : O(log(n)).

0 commit comments

Comments
 (0)