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

不了解这个数学原理的,面试时很难做出来。

posted on 2019-12-11 10:03  Sempron2800+  阅读(194)  评论(0编辑  收藏  举报