1 class Solution: 2 def grayCode(self, n: int) -> 'List[int]': 3 gray = [0] 4 for i in range(n): 5 # reflect, add leading 1, and concatenate 6 gray.extend([(1 << i) + x for x in gray[::-1]]) 7 return gray
这是一道数学题,参考:https://leetcode.com/problems/gray-code/discuss/445279/Python-solution
不了解这个数学原理的,面试时很难做出来。