89. 格雷编码
摘自[1][2][3]:
G:格雷码 B:二进制码 n:正在计算的位
根据格雷码的定义可得:
G(n) = B(n+1) XOR B(n)
AC code:
// Binary to grey code
class Solution {
public:
vector<int> grayCode(int n) {
vector<int>res;
for (int i = 0; i < pow(2, n); i++)
{
res.push_back((i >> 1) ^ i);
}
return res;
}
};
[1]https://blog.csdn.net/w8253497062015/article/details/80896500
[2]https://www.cnblogs.com/grandyang/p/4315649.html
[3]https://zh.wikipedia.org/wiki/格雷码