Share my Python Solution

  • 1

    We add 2^i to the corresponding number in the reversed copy of the previous array, then append it to the array each time.

    class Solution:
        # @return a list of integers
        def grayCode(self, n):
            a = [0]
            for i in range(n):
                a += list(x + 2**i for x in a[::-1])
            return a

Log in to reply

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