题目:https://leetcode-cn.com/problems/gray-code/ 参考:https://leetcode-cn.com/problems/gray-code/solution/gray-code-jing-xiang-fan-she-fa-by-jyd/
class Solution {
public:
vector grayCode(int n) {
/*
*镜像反射法
*令G[n]表示阶数为n的格雷编码
*那么G[n+1]等于G[n] + reverse(G[n])+高位1
*/
vector ans;
ans.push_back(0);
int res = 1;
for(int k = 1;k = 0;i--)
ans.push_back(res+ans[i]);
res
关注
打赏
热门博文