Skip to content

[386] Lexicographical Numbers

https://leetcode.com/problems/lexicographical-numbers/description/

  • algorithms
  • Medium (43.69%)
  • Source Code: 386.lexicographical-numbers.py
  • Total Accepted: 38.6K
  • Total Submissions: 85K
  • Testcase Example: '13'

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.

python
class Solution(object):
    def lexicalOrder(self, n):
        """
        :type n: int
        :rtype: List[int]
        """
        pool = list(range(1, n+1))
        pool.sort(key=lambda x: str(x))
        return pool
#
#
# s = Solution()
# print s.lexicalOrder(13)
#

Last updated: