386.Lexicographical Numbers

Given an integer n, return 1 - n in lexicographical order.

For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9].

Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000.

class Solution:
    def lexicalOrder(self, n):
        """
        :type n: int
        :rtype: List[int]
        """
        list = []
        for i in range(1,n+1):
            list.append(i)
        list.sort(key = lambda x:str(x))
        return list
posted @ 2018-10-03 14:06  bernieloveslife  阅读(93)  评论(0编辑  收藏  举报