Python recursive solution - easy understanding


  • 3
    G
    class Solution(object):
    
    def grayCode(self, n):
        """
        :type n: int
        :rtype: List[int]
    
        """
        if n<1:
            return [0]
        if (n==1):
            return [0,1]
        res = self.grayCode( n-1)
        x=pow(2,n-1)
        for i in range(x-1,-1,-1):
            res.append(res[i]+x)
        return res

Log in to reply
 

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