-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1215. Stepping Numbers.py
More file actions
57 lines (40 loc) · 1.11 KB
/
Copy path1215. Stepping Numbers.py
File metadata and controls
57 lines (40 loc) · 1.11 KB
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
54
55
56
57
class Solution:
def countSteppingNumbers(self, low: int, high: int) -> List[int]:
result = []
# base case of 0
if low == 0:
result.append(0)
queue = deque([1,2,3,4,5,6,7,8,9])
while queue:
num = queue.popleft()
# case when we finish, ie. we start getting numbers greater than high
if num > high:
return result
# include numbers from low --> high inclusive
if num >= low:
result.append(num)
prev_ones_place = num % 10
# now add another place, and add +- 1 to that new ones place
# make sure they append in order
if prev_ones_place > 0:
queue.append(num * 10 + prev_ones_place - 1)
if prev_ones_place < 9:
queue.append(num * 10 + prev_ones_place + 1)
'''
we can use BFS
think of tree like decisions
1
| \
10 12
15
| \
154 156
start with a queue of 1 - 9
we generate numbers and if its between low and high, append result
10
| \
x 101
9
| \
98 x
'''