Share my Python solution


  • 0
    class Solution(object):
        def grayCode(self, n):
            if n == 0: return [0]
            r = [0, 1]
            for i in range(1, n):
                l = len(r)
                for j in range(l-1, -1, -1):
                    r.append((1<<i) + r[j])
            return r
    

Log in to reply
 

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