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位的格雷码相应的二进制数,主要在于找到一种格雷码的递增方法(格雷码并非唯一的,能够 ...
随机推荐
- lucene索引的更新和删除
索引的删除: IndexReader和IndexWriter都由删除索引的功能,但这两者是有区别的, 使用IndexReader删除索引时,索引会马上被删除,其有两种方法,可以删除索引deleteDo ...
- Windows 计划任务之消息提醒
Windows 计划任务之消息提醒 你肯定也有这种需求.想做一个计划任务,却发现老式消息提醒已经被微软禁止了. 或者就是很单纯的希望给系统弹出一个消息框而并非CMD的echo命令. so...how ...
- python3的嵌套函数
背景 最近在学python3 嵌套函数 顾名思义,即使在函数中还有函数,实现了函数的多级嵌套 def test1(): age = 10 print(age) def test2(): te = 5 ...
- C#-WebForm-★ASP.NET中的母版页★
何为母版页,顾名思义母版就是模版,就像在PPT里面的板式或主题一样,大框架已经有了,我们的任务就是向里面添加具体的内容.这样我们制作的所有幻灯片的外观大体都是一样的. 在ASP.NET中母版页有两种作 ...
- Java 访问权限控制:你真的了解 protected 关键字吗?
摘要: 对于类的成员而言,其能否被其他类所访问,取决于该成员的修饰词:而对于一个类而言,其能否被其他类所访问,也取决于该类的修饰词.在Java中,类成员访问权限修饰词有四类:private,无(包访问 ...
- Linux文件索引节点相关概念
一. 概念 1. inode(index node)表中包含文件系统所有文件列表 一个节点 (索引节点)是在一个表项,包含有关文件的信息( 元数据 ),包括: 文件类型,权限,UID,GID 链接 ...
- vue.js - axios Get、Post方法传参给 .net core webapi。
一:axios中是 Get请求: 1:在vue项目中通过params属性携带数据: let _self = this; axios({ method:'get', url:'http://localh ...
- 没事用html5 canvas画一个仪表盘自用,自适应的哦
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 完全国人自主研发原创的智能软件路由器BDS即将发布,附带企业服务总线ESB功能
完全国人自主研发原创的智能软件路由器即将发布: 完全国人自主研发原创的智能软件路由器BDS即将发布,附带企业服务总线ESB功能 智能软件路由器 BDS 简要介绍 http://kan.weibo.co ...
- CKEditor图片上传实现详细步骤(使用Struts 2)
本人使用的CKEditor版本是3.6.3.CKEditor配置和部署我就不多说. CKEditor的编辑器工具栏中有一项“图片域”,该工具可以贴上图片地址来在文本编辑器中加入图片,但是没有图片上传. ...