本文共 759 字,大约阅读时间需要 2 分钟。
题目描述
示例 1:输入: 2
输出: [0,1,3,2] 解释: 00 - 0 01 - 1 11 - 3 10 - 2对于给定的 n,其格雷编码序列并不唯一。
例如,[0,2,3,1] 也是一个有效的格雷编码序列。00 - 0
10 - 2 11 - 3 01 - 1来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/gray-code 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。C++
class Solution { public: /* 格雷编码的生成 G(i)=i ^ (i/2) 如 n = 3: G(0) = 000, G(1) = 1 ^ 0 = 001 ^ 000 = 001 G(2) = 2 ^ 1 = 010 ^ 001 = 011 G(3) = 3 ^ 1 = 011 ^ 001 = 010 G(4) = 4 ^ 2 = 100 ^ 010 = 110 G(5) = 5 ^ 2 = 101 ^ 010 = 111 G(6) = 6 ^ 3 = 110 ^ 011 = 101 G(7) = 7 ^ 3 = 111 ^ 011 = 100 */ vector grayCode(int n) { vector res; for(int i=0;i< 1<>1); return res; }};