Skip to content

Latest commit

 

History

History
23 lines (21 loc) · 539 Bytes

386.md

File metadata and controls

23 lines (21 loc) · 539 Bytes

386. Lexicographical Numbers

Solution 1 (time O(n), space O(1))

class Solution(object):
    def lexicalOrder(self, n):
        """
        :type n: int
        :rtype: List[int]
        """
        ans = [0 for _ in range(n)]
        num = 1
        for i in range(n):
            ans[i] = num
            if num * 10 <= n:
                num *= 10
            else:
                while num % 10 == 9 or num + 1 > n:
                    num //= 10
                num += 1
        return ans