Skip to content

Commit 9371a0a

Browse files
1291. Sequential Digits
Difficulty: Medium 21 / 21 test cases passed. Runtime: 28 ms Memory Usage: 13.8 MB
1 parent 5408a74 commit 9371a0a

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Medium/1291.SequentialDigits.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
An integer has sequential digits if and only if each digit in the number is
3+
one more than the previous digit.
4+
Return a sorted list of all the integers in the range [low, high] inclusive
5+
that have sequential digits.
6+
7+
Example:
8+
Input: low = 100, high = 300
9+
Output: [123,234]
10+
11+
Example:
12+
Input: low = 1000, high = 13000
13+
Output: [1234,2345,3456,4567,5678,6789,12345]
14+
15+
Constraints:
16+
- 10 <= low <= high <= 10^9
17+
"""
18+
#Difficulty: Medium
19+
#21 / 21 test cases passed.
20+
#Runtime: 28 ms
21+
#Memory Usage: 13.8 MB
22+
23+
#Runtime: 28 ms, faster than 77.18% of Python3 online submissions for Sequential Digits.
24+
#Memory Usage: 13.8 MB, less than 52.82% of Python3 online submissions for Sequential Digits.
25+
26+
class Solution:
27+
def sequentialDigits(self, low: int, high: int) -> List[int]:
28+
i = 0
29+
j = len(str(low))
30+
num = 0
31+
result = []
32+
digits = "1234567890"
33+
length = len(digits)
34+
while num < high:
35+
num = int(digits[i:i+j])
36+
if num in range(low, high+1):
37+
result.append(num)
38+
i += 1
39+
if i + j >= length:
40+
j += 1
41+
i = 0
42+
return result

0 commit comments

Comments
 (0)