-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsequential-digits.py
More file actions
35 lines (29 loc) · 1.01 KB
/
sequential-digits.py
File metadata and controls
35 lines (29 loc) · 1.01 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
from typing import List
import math
class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
base = "123456789"
low_d, high_d = int(math.log10(low))+1, int(math.log10(high))+1
res = []
for length in range(low_d, high_d+1):
for i in range(9-length+1):
if low <= (num := int(base[i:i+length])) <= high:
res.append(num)
return res
def sequentialDigits2(self, low: int, high: int) -> List[int]:
n = len(str(high))
def backtrack(num, curr, res):
if len(curr) > n:
return
if num == 9:
return
curr += str(num+1)
curr_num = int(curr)
if low <= curr_num <= high:
res.append(curr_num)
backtrack(num+1, curr, res)
res = []
first = 1
for i in range(first, 10):
backtrack(i, str(i), res)
return list(sorted(res))