My python solution with stack


  • 0
    G
    enter code hereclass Solution(object):
    def grayCode(self, n):
        """
        :type n: int
        :rtype: List[int]
        """
        res=[0]
        if n==0:
            return res
            
        for i in range(1,n+1):
            self.helper(res,i)
            
        return res
            
    def helper(self,res,n):
        temp=[]
        for i in res:
            temp.append(i+2**(n-1))
        l=len(res)
        for i in range(0,l):
            res.append(temp.pop())

Log in to reply
 

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