此题将 n 的格雷码求出,首先要知道格雷码的含义,即相邻位之间只有一位不同
代码
class Solution {
public:
vector<int> grayCode(int n) {
// 共有 2^n个格雷码
int size = 1<<n;
vector<int> result;
for(int i = 0; i < size; ++i)
{
result.push_back((i>>1)^i);
}
return result;
}
};
参考http://fisherlei.blogspot.com/2012/12/leetcode-gray-code.html
本文详细解释了如何生成雷码,即相邻位之间只有一位不同的序列,并提供了C++实现的示例代码。
383

被折叠的 条评论
为什么被折叠?



