Fast and simple Python Solution based on bitwise operation


  • 0
    M
    class Solution(object):
        def grayCode(self, n):
            """
            :type n: int
            :rtype: List[int]
            """
            if n < 1:
                return [0]
            
            ret = [0, 1]
            for i in xrange(1, n):
                r_seq = [j ^ 2**i for j in reversed(ret)]
                ret.extend(r_seq)
                
            return ret

Log in to reply
 

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