美文网首页
386. Lexicographical Numbers

386. Lexicographical Numbers

作者: 阿团相信梦想都能实现 | 来源:发表于2016-09-22 00:43 被阅读0次
    class Solution(object):
        def lexicalOrder(self, n):
            """
            :type n: int
            :rtype: List[int]
            """
            #comments are based on counting to 150
            res=[1]
            while len(res)<n:
                new=res[-1]*10 #add 1,10,100
                while new>n:#loop through numbers such as 101-109 
                    new/=10
                    new+=1
                    while new%10==0: #increase the base from 10 to 11 
                        new/=10
                res.append(new)
                
            return res
    

    相关文章

      网友评论

          本文标题:386. Lexicographical Numbers

          本文链接:https://www.haomeiwen.com/subject/kxfzettx.html