My one-line python solution


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

    I know it's not solution with best performance, but it's beautiful.


  • 5
    D

    one-line Python solution without recursion.

    class Solution:
        # @return a list of integers
        def grayCode(self, n):
            return [i ^ (i >> 1) for i in range(1 << n)]

  • 0

    It's much better!


Log in to reply
 

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