An integer has sequential digits if and only if each digit in the number is one more than the previous digit. Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.
Example 1: Input: low = 100, high = 300 Output: [123,234] Example 2: Input: low = 1000, high = 13000 Output: [1234,2345,3456,4567,5678,6789,12345]
- code #slidingwindow
class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
sample = "123456789"
n = 10
nums = []
for length in range(len(str(low)), len(str(high)) + 1):
for start in range(n - length):
num = int(sample[start: start + length])
if low <= num <= high:
nums.append(num)
return nums
- code naive
class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
def nex(num):
num_len = len(str(num))
if num % 10 != 9:
num += int('1' * num_len)
else:
num = nexend = 1
while len(str(num)) < num_len + 1:
nexend += 1
num = num * 10 + nexend
return num
def findfirst(low):
num_len = len(str(low))
num = nexend = 1
while len(str(num)) < num_len:
nexend += 1
num = num * 10 + nexend
if num > low: return num
while low > num:
num = nex(num)
return num
res = []
num = findfirst(low)
while num <= high:
res.append(num)
num = nex(num)
return res