class Solution {
public:
vector<int> grayCode(int n) {
vector<int> res;
res.push_back(); long pow2 = ;
for (int i=; i <= n; i++) {
int old_len = res.size();
for (int i=; i<old_len; i++) {
res.push_back(pow2 + res[old_len - i - ]);
}
pow2<<=;
}
return res;
}
};

再水一发, 不过n==0时,觉得应该返回一个空的vector

简化版:

class Solution {
public:
vector<int> grayCode(int n) {
vector<int> res;
res.push_back(); for (int i=; i<n; i++) {
int idx = res.size() - ;
while (idx >= ) {
res.push_back(res[idx--] | <<i);
}
} return res;
}
};

递归方法:

class Solution {
public:
vector<int> grayCode(int n) {
vector<int> res;
if (n < ) {
res.push_back();
return res;
} else if (n == ) {
res.push_back();
res.push_back();
return res;
}
vector<int> sub = grayCode(n - );
int len = sub.size();
for (int i=; i < len; i++) {
res.push_back(sub[i]);
}
for (int i=len-; i>=; i--) {
res.push_back((<<(n-))|sub[i]);
}
return res;
}
};

LeetCode GrayCode的更多相关文章

  1. leetcode — gray-code

    import org.lep.leetcode.groupanagrams.GroupAnagram; import java.util.ArrayList; import java.util.Arr ...

  2. [LeetCode] Gray Code 格雷码

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  3. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  4. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  5. LeetCode题目分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  6. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  7. [LeetCode]题解(python):089 Gray Code

    题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...

  8. <转>LeetCode 题目总结/分类

    原链接:http://blog.csdn.net/yangliuy/article/details/44514495 注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http:/ ...

  9. Gray Code -- LeetCode

    原标题链接: http://oj.leetcode.com/problems/gray-code/  这道题要求求出n位的格雷码相应的二进制数,主要在于找到一种格雷码的递增方法(格雷码并非唯一的,能够 ...

随机推荐

  1. Cocoa对象——根类

    [转载自:http://mobile.51cto.com/iphone-274229.htm] Cocoa对象 根类是本文要介绍的内容,仅凭Objective-C语言和运行环境并不足以构造哪怕是最简单 ...

  2. gitLab创建自己的私有库

    一.创建私有库的流程简介 创建一个项目,留着后面的流程3制作私有库 在可以创建私有库的地方创建一个code repository, code repository是代码仓库,我们把代码上传到这个仓库. ...

  3. BootStrap Modal 点击空白时自动关闭

    本文为大家讲解的是如何禁用 BootStrap Modal 点击空白时自动关闭的方法,感兴趣的同学参考下. 方法如下 $('#myModal').modal({backdrop: 'static', ...

  4. 封装 原生 fetch

    1, 简介 fetch方法是 Fetch API的一个方法,提供了一种简单.合理的方式来跨网络异步获取资源. 与原来的XMLHttpRequest比较,fetch更容易与其他的技术结合:比如servi ...

  5. springcloud(八)-Hystrix熔断器

    雪崩效应 在微服务架构中通常会有多个服务层调用,基础服务的故障可能会导致级联故障,进而造成整个系统不可用的情况,这种现象被称为服务雪崩效应.服务雪崩效应是一种因“服务提供者”的不可用导致“服务消费者” ...

  6. 认识python正则模块re

    python正则模块re python中re中内置匹配.搜索.替换方法见博客---python附录-re.py模块源码(含re官方文档链接) 正则的应用是处理一些字符串,phthon的博文python ...

  7. kubernetes 与LVM的结合

    本文主要介绍k8s与LVM结合使用的场景,在原生的k8s中对于本地存储提供了hostPath与emptyDir两种volme,hostPath是直接将文件存储在本地主机上,存在的问题是无法进行quot ...

  8. android studio2.3.3 模拟器 Jni函数调用C++对象,lldb调试this指针和相关变量显示无效的原因

    android studio2.3.3 的版本中 Jni函数调用C++对象,对象调用相关的成员函数, lldb调试,变量跟踪窗口,this指针和相关变量显示无效的原因,但这些参数实际是有效的,只是de ...

  9. 【转载】伪静态SQL注入

    伪静态,主要是为了隐藏传递的参数名,伪静态只是一种URL重写的手段,既然能接受参数输入,所以并不能防止注入.目前来看,防止注入的最有效的方法就是使用LINQ.常规的伪静态页面如下:http://www ...

  10. JavaScript数据结构-19.拓扑排序

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...