leetcode-167周赛-1291-顺次数

题目描述:

 

 自己的提交:

class Solution:
    def sequentialDigits(self, low: int, high: int) -> List[int]:
        l,h = len(str(low)),len(str(high))
        res = []
        def helper(num,length):
            if length == 0 and low <= num <= high:
                res.append(num)
                return
            if num % 10 == 9:
                return
            helper(num*10+(num%10+1),length-1)
        for i in range(l,h+1):
            for j in range(1,10):
                helper(j,i-1)
        return res

另:

class Solution:
    def sequentialDigits(self, low: int, high: int) -> List[int]:
        ans = list()
        for i in range(1, 10):
            num = i
            for j in range(i + 1, 10):
                num = num * 10 + j
                if low <= num <= high:
                    ans.append(num)
        return sorted(ans)
posted @ 2019-12-24 14:30  oldby  阅读(242)  评论(0编辑  收藏  举报