-
Notifications
You must be signed in to change notification settings - Fork 613
/
1291.py
53 lines (44 loc) · 1.47 KB
/
1291.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'''
An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.
Example 1:
Input: low = 100, high = 300
Output: [123,234]
Example 2:
Input: low = 1000, high = 13000
Output: [1234,2345,3456,4567,5678,6789,12345]
'''
class Solution(object):
def sequentialDigits(self, low, high):
"""
:type low: int
:type high: int
:rtype: List[int]
"""
result = []
start = int(str(low)[0])
for val in range(1, len(str(low))):
new_val = start%10 + 1
start = start*10 + new_val
if start > high:
return result
result.append(start)
while result[-1] <= high:
temp = str(result[-1])
next_elem = int(temp[-1]) + 1
if next_elem > 9:
next_greater = 0
for index in range(len(temp) + 1):
next_greater = next_greater*10 + (index+1)
else:
next_greater = int(temp[1:]) * 10 + next_elem
if next_greater <= high:
result.append(next_greater)
else:
break
# print next_greater
final_result = []
for val in result:
if '0' not in str(val) and val >= low:
final_result.append(val)
return final_result