-
Notifications
You must be signed in to change notification settings - Fork 17
/
sequential-digits.py
38 lines (26 loc) · 966 Bytes
/
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
34
35
36
37
38
from typing import List
class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
def digits(num: int) -> int:
count = 0
while num > 0:
num //= 10
count += 1
return count
def dfs(low: int, high: int, length: int, num: int) -> List[int]:
if num * 10 ** length + 10 ** length - 1 < low:
return []
if num * 10 ** length > high:
return []
if length == 0:
return [num]
result = []
next_num = num % 10 + 1
if next_num != 10:
result.extend(dfs(low, high, length - 1, num * 10 + next_num))
return result
result = []
for length in range(digits(low), digits(high) + 1):
for num in range(1, 10):
result.extend(dfs(low, high, length - 1, num))
return result