-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1291.Sequential-Digits.py
33 lines (28 loc) · 1.01 KB
/
1291.Sequential-Digits.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
# generate a integer that has sequential digits with the leftmost number start and a fixed length
# return -1 if impossibe
def generateSeq(start, length):
res = 0
while length:
if start == 0 or start == 10:
return -1
res += start * (10 ** (length - 1))
start += 1
length -= 1
return res
length = len(str(low))
first = low // (10 ** (length - 1))
res = []
while first * (10 ** (length - 1)) <= high:
# generate the next integer
num = generateSeq(first, length)
if num == -1:
# we need to increase the length
length += 1
first = 1
continue
elif num >= low and num <= high:
res.append(num)
first += 1
return res