The most elegant python solution so far. 10 liner. iterative. O(n) time, O(1) space.


  • 15
    W

    Simple. easy to understand. No fking weird, rarely used built-int functions.
    '''

    class Solution(object):
        def lexicalOrder(self, n):
            ans = [1]
            while len(ans) < n:
                new = ans[-1] * 10
                while new > n:
                    new /= 10
                    new += 1
                    while new % 10 == 0:    # deal with case like 199+1=200 when we need to restart from 2.
                        new /= 10
                ans.append(new)    
            return ans
    

    '''


  • 0
    W

    This is totally AWESOME!!!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.