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位的格雷码相应的二进制数,主要在于找到一种格雷码的递增方法(格雷码并非唯一的,能够 ...
随机推荐
- javassist学习
最近学习了javassist的使用方法. javassist是一个字节码类库,可以用他来动态生成类,动态修改类等等. 下面是如果用javassist来动态创建一个类的demol 我们需要创建的目标类, ...
- CentOS6.5下telnet服务
00×0 本文介绍Telnet搭建,以及展示这是一个不安全的远程服务. 00×1 服务准备工作 [root@localhost ~]# yum install xinetd telnet-server ...
- 孩子们各显神通对付 iOS 12「屏幕使用时间」的限制
简评:2018 年秋季,苹果公司推出了 iOS 12,其中备受好评的一项改变是:增加了屏幕使用时间限制,以减轻沉迷手机的状况.三个月过去后,这项功能似乎并没有对孩子造成太多困扰,道高一尺魔高一丈,孩子 ...
- 前后端分离——token超时刷新策略
前言 记录一下前后端分离下————token超时刷新策略! 需求场景 昨天发了一篇记录 前后端分离应用——用户信息传递 中介绍了token认证机制,跟几位群友讨论了下,有些同学有这么一个疑惑:toke ...
- leetcode-217-Contains Duplicate(使用排序来判断整个数组有没有重复元素)
题目描述: Given an array of integers, find if the array contains any duplicates. Your function should re ...
- 阿里云服务器18个数据中心测试IP地址以及测试方法
我们用户在选择阿里云服务器的时候是不是感觉阿里云的数据中心太多太多,确实阿里云服务器机房是有很多,国外国外机房大约有18个,甚至更多,因为还在不断的增加机房.对于商家而言增加不同的机房可以满足不同的项 ...
- 利用Android Studio编写 Android上的c与c++程序
利用Android Studio编写 Android上的c与c++程序 (2017-05-22 19:01:20) 转载▼ 标签: android 分类: Android开发 原文链接: http:/ ...
- VS 代码自动对齐快捷键
全部代码代码自动对齐快捷键为 Ctrl + a(按后可松松手) + k(按后可松松手) + f
- javascript绑定事件addEventListener与attachEvent
1.eleObj.addEventListener(eventName,handle,useCapture); eleObj:DOM元素: eventName:事件名称.注意,这里的事件名称没有“ o ...
- Hibernate3.3.2_ID生成策略
1,xml生成id generator:常用四个:native.identity.sequence.uuid. Annotation: 1,@GeneratedValue: a)自定义ID b)AUT ...