LeetCode 1291 Sequential Digits (Python)

Posted by 小明MaxMing on September 19, 2020

题目

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.

解题思路

如果一个数符合条件,一定是“123456789”的一个连续子串,先枚举这个数的位数,再枚举长度,如果当前的数大于high,就可以停止了

代码

class Solution:
    def sequentialDigits(self, low: int, high: int) -> List[int]:
        seq = '123456789'
        res = []
        for l in range(1, 10):
            for i in range(len(seq) - l + 1):
                num = int(seq[i: i + l])
                if num > high:
                    return res
                if num >= low:
                    res.append(num)
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅