forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1215.py
25 lines (23 loc) · 736 Bytes
/
1215.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
class Solution:
def countSteppingNumbers(self, l: int, r: int) -> List[int]:
res = []
def bfs(s):
q = [s]
while len(q):
pre = q.pop(0)
if pre <= r and pre >= l:
res.append(pre)
if pre == 0 or pre > r:
return
lt = pre % 10
s1, s2 = pre * 10 + lt - 1, pre*10 + lt + 1
if lt == 0:
q.append(s2)
elif lt == 9:
q.append(s1)
else:
q.append(s1)
q.append(s2)
for i in range(10):
bfs(i)
return sorted(res)