LeetCode GrayCode
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的更多相关文章
- leetcode — gray-code
import org.lep.leetcode.groupanagrams.GroupAnagram; import java.util.ArrayList; import java.util.Arr ...
- [LeetCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- leetcode算法分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode题目分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- [LeetCode]题解(python):089 Gray Code
题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...
- <转>LeetCode 题目总结/分类
原链接:http://blog.csdn.net/yangliuy/article/details/44514495 注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http:/ ...
- Gray Code -- LeetCode
原标题链接: http://oj.leetcode.com/problems/gray-code/ 这道题要求求出n位的格雷码相应的二进制数,主要在于找到一种格雷码的递增方法(格雷码并非唯一的,能够 ...
随机推荐
- 私有成员的设置和访问方式——setter和getter
在定义类时,为了保证类中成员数据安全性及的封装性,防止成员数据值被任意修改,通常将类中成员属性用private进行修饰. 被private修改的成员属性,只能在类中访问,跳出本类后,就无法直接访问. ...
- PHP 访问链接的3种方式
对于php访问url的方法比价多,对于一些防护比较低的网站,可以轻易的实现刷网站浏览量的可能 1.fopen方式 function access_url($url) { if ($url=='') r ...
- Make ISO安装ArchLinux加Cinnamon
Arch安装一直对大家对普通用户来說一直很难.国外大神为Arch安装进行了优化提供了更方便的安装方式 官网:http://www.evolutionlinux.com/ 以下爲个人理解,供大家参考. ...
- SpringMvc HttpMessageConverter之@ResponseBody
我们先看HttpMessageConverter的示意图,从图片可以看出它是多么的重要.在一条必经之路截道了的感觉. 先上我的测试例子: jsp页面: <%@ page language=&qu ...
- null、 is_null() 、empty() 、isset() PHP 判断变量是否为空
PHP中,在判断变量是否为空的时候,总会纠结应该选用哪个函数,下面列取常用的多种情况,其中1/3经过我的验证,其它来自网络,验证后使用... 使用 PHP 函数对变量 $x 进行比较 表达式 gett ...
- python3.6的request
request实例1: import requests payload = {'key1':'value','key2':'value2'} url = "http://httpbin.or ...
- 常用的re正则
常用的正则表达式: 用户名:/^[a-z0-9_-]{3,16}$/ 密码:/^[a-z0-9_-]{6,18}$/ 十六进制值:/^#?([a-f0-9]{6}|[a-f0-9]{3})$/ 电子邮 ...
- shortcut&website
作者:Vincent链接:https://www.zhihu.com/question/28993252/answer/61618961来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转 ...
- (转)DB2和 Oracle的并发控制(锁)比较
DB2和 Oracle的并发控制(锁)比较 牛 新庄2005 年 12 月 26 日发布 原文:https://www.ibm.com/developerworks/cn/data/library/t ...
- Java之重载(Overload)与重写(Overwrite)总结
内容来源为:<孙卫琴面向对象编程>,本随笔简单总结,具体内容可参见概述第6章,写的挺清晰: 一. 重载(Overload) 1. 有时候类的同一种功能有多种实现方式,到底采用哪种实现方式, ...