Python with recursive solution

  • 0

    Share my recursive python solution.
    Try to start from 1, then try 10 , 100....
    If multi 10 exceed the limitation, try to add 1 and the backward.
    In the add 1 step, if I don't set base to limit the range to 10, it would step over.
    such as n = 11, without base limitation would return [1,10,11,2,3,4,5,6,7,8,9,10,11]

    class Solution(object):
        def lexicalOrder(self, n):
            :type n: int
            :rtype: List[int]
            ans = []
            recur(ans, n, 1, 0);
            return ans
    def recur(ans, n, temp, base):
        if(temp%10 == 0):
            base = temp
        if(temp*10 <= n):
            recur(ans, n, temp*10, base)
        if(temp+1 <= n and temp+1 < base+10):
            recur(ans, n, temp+1, base)

Log in to reply

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