Python solution in 48 ms


  • 0
    C
    class Solution:
        # @return a list of integers
        def grayCode(self, n):
            if n < 1: return [0, ]
            if n == 1: return [0, 1]
    
            bit = 1 << (n - 1)
            lower = self.grayCode(n - 1)
            return lower + [(bit | i) for i in reversed(lower)]

Log in to reply
 

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