Gray code

September 3, 2022

bit-manipulation

Problem URL: Gray code

There is formula to get Gray's code, for every number i, it i ^ (i>>1). We can use the foumula to get the values for 2^n numbers and return it as the result.

class Solution:
    def grayCode(self, n: int) -> List[int]:
        res = []
        for i in range(2**n):
            res.append(i ^ (i>>1))
        return res

Time Complexity: O(2^n)
Space Complexity: O(1)