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.
class Solution:
def sequentialDigits(self, low, high):
res = []
# 限定数字的位数
for length in range(len(str(low)), len(str(high)) + 1):
# 开头第一个数字
for i in range(10 - length):
num = 0
# 以这个数字开头的所有长度
for j in range(i, i + length):
num = num * 10 + j + 1
# 符合要求加进去,超出范围直接 break,开始下一个数字的循环
if low <= num <= high:
res.append(num)
elif num > high:
break
return res