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位的格雷码相应的二进制数,主要在于找到一种格雷码的递增方法(格雷码并非唯一的,能够 ...
随机推荐
- 数据分析:pandas 基础
pandas 是基于 Numpy 构建的含有更高级数据结构和工具的数据分析包 类似于 Numpy 的核心是 ndarray,pandas 也是围绕着 Series 和 DataFrame 两个核心数据 ...
- Oracle中ROWNUM的使用技巧
ROWNUM是一种伪列,它会根据返回记录生成一个序列化的数字.利用ROWNUM,我们可以生产一些原先难以实现的结果输出,但因为它是伪列的这个特殊性,我们在使用时也需要注意一些事项,不要掉入“陷阱”.下 ...
- Scala代码开发 metaTable(元表)
使用Scala语言开发时,自动生成get和set方法 不用写return进行返回, 因为它的最后一行就是返回值 先建立四个层(层层递进) domain 表结构的建立 repository(DAO) 实 ...
- Linux基础篇–shell脚本编程基础
本章内容概要 编程基础 脚本基本格式 变量 运算 条件测试 配置用户环境 7.1 编程基础程序:指令+数据程序编程风格: 过程式:以指令为中心,数据服务于指令 对象式:以数据为中心 ...
- mysql中字符串1.1/1.2/1.2.2/1.2.5排序问题
1.创建查询函数:(split_pid为函数名称) create function split_pid(str varchar (1000),delimiter varchar(1)) returns ...
- 【Three.js】模型抗锯齿处理
1.锯齿消除方法 three.js参考使用官方demo发现模型渲染有锯齿,这种情况在旋转视角时候就非常明显. 抗锯齿的方法,很简单,只需要配置render两个属性即可: renderer = new ...
- (转)一张图学会Dockerfile
原文:http://blog.51cto.com/kusorz/1942816 前言 Dockerfile是非常容易学的,和SHELL相比那简单的太多了. Dockerfile是为快速构建docker ...
- 静态代理,动态代理,Cglib代理详解
一.静态代理 新建一个接口 定义一个玩家方法: package com."".proxy.staticc; public interface Iplayer { public vo ...
- redis 实现消息发布和订阅
1,打开二个客户端机器 一个用于发布,一个用于接受 2,发布一个channel1 3,用另外一个客户端收听上面的客户端 4,当再次在发布的redis客户端 发布一个消息 其他所有订阅的客户端会自动收 ...
- word-wrap/word-break/white-space
word-wrap语法: word-wrap : normal | break-word normal : 默认值,单词如果单词超长,会冲出边界(单个单词超长,在当前行显示) break-word : ...