本文共 357 字,大约阅读时间需要 1 分钟。
这个题涉及到格雷码,如果你从没了解过格雷码,请戳。
我用到的方法也是最简单的二进制码和二进制格雷码转换。class Solution: def grayCode(self, n): """ :type n: int :rtype: List[int] """ resultCodeList = [] for i in range(0,2 ** n): # 右移然后异或 grayCode = (i >> 1)^i resultCodeList.append(grayCode) return resultCodeList