Leetcode刷題筆記-Array2

386. Lexicographical Numbers

 

class Solution(object):
    def lexicalOrder(self, n):
        def dfs(num):
            if num > n: return
            res.append(num)
            for i in xrange(10):
                if num*10 + i <= n:
                    dfs(num*10+i)
        
        res = []
        for i in xrange(1, 10):  # 這裏只需要遍歷1~9
            dfs(i)
        return res

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章