class Solution(object): def grayCode(self, n): return [x ^ (x >> 1) for x in range(0, 1 << n)]
It's just observation. new_x = x ^ x >> 1. I won't be able to come up with this if new to gray code.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.