Gray Code

如果知道格雷码本身的规律的话,这道题可以直接算出每个数字和它对应的标准格雷码(因为格雷码组合排列并不是唯一的)

class Solution(object):
    def grayCode(self, n):
        """
        :type n: int
        :rtype: List[int]
        """
        result = []
        for i in xrange(int(2 ** n)):
            result.append(i ^ (i >> 1))
        return result

暴力穷举的回溯法思路稍微有点绕,暂时不想了

results matching ""

    No results matching ""