HanchengZhao / algorithm-notes

0 stars 0 forks source link

89. Gray Code #11

Open HanchengZhao opened 7 years ago

HanchengZhao commented 7 years ago

89. Gray Code

HanchengZhao commented 7 years ago
class Solution(object):
    def grayCode(self, n):
        """
        :type n: int
        :rtype: List[int]
        """
        # grayCode is symmetric
        # each time you want to expand to one more digit, just add 0 on the left for each element
        # in the array and add 1 for the reversed array to keep symmetric
        res = [0]
        for i in xrange(n):
            for element in res[::-1]:
                res.append(2 ** i + element)
        return res