class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
res = []
for num in range(low, high + 1):
s = str(num)
flag = True
for i in range(1, len(s)):
if ord(s[i]) - ord(s[i - 1]) != 1:
flag = False
break
if flag:
res.append(num)
return resclass Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
res = []
low_digit, high_digit = len(str(low)), len(str(high))
for digits in range(low_digit, high_digit + 1):
for start in range(1, 10):
if start + digits > 10:
break
num = start
prev = start
for i in range(digits - 1):
num = num * 10
prev += 1
num += prev
if low <= num <= high:
res.append(num)
return resSince, we have at most valid numbers as per the given constraints.
class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
res = []
queue = deque(range(1, 10))
while queue:
n = queue.popleft()
if n > high:
continue
if low <= n <= high:
res.append(n)
ones = n % 10
if ones < 9:
queue.append(n * 10 + (ones + 1))
return resSince, we have at most valid numbers as per the given constraints.
class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
res = []
def dfs(num):
if num > high:
return
if low <= num <= high:
res.append(num)
last_digit = num % 10
if last_digit < 9:
dfs(num * 10 + (last_digit + 1))
for i in range(1, 10):
dfs(i)
return sorted(res)Since, we have at most valid numbers as per the given constraints.
class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
nums = "123456789"
res = []
for d in range(2, 10):
for i in range(9 - d + 1):
num = int(nums[i: i + d])
if num > high:
break
if low <= num <= high:
res.append(num)
return resSince, we have at most valid numbers as per the given constraints.