LeetCode OJ 89. Gray Code
题目
The gray code is a binary numeral system where two successive values differ in only one bit.
Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.
For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:
00 - 0
01 - 1
11 - 3
10 - 2
Note:
For a given n, a gray code sequence is not uniquely defined.
For example, [0,2,3,1] is also a valid gray code sequence according to the above definition.
For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.
解答
这就是离散数学里面一个格雷码的快速推导算法。。。
比如2位格雷码是00 01 11 10,这个数列奇数项左移后先加0再加1,偶数项左移后先加1再加0,按顺序组成的数列就是3位格雷码000 001 011 010 110 111 101 100。原理很简单,因为本来就是两两相邻之间差一位,现在按照这样操作,左移扩展一位,而新加的这一位又是01100110,这样原来每个数都扩展为相邻差一位,而原来相邻差一位的两个数之间加的是同样的一位数。
下面是AC的代码:
class Solution {
public:
vector<int> grayCode(int n) {
vector<int> ans;
vector<int> temp;
ans.push_back(0);
for(int i = 0; i < n; i++){
int length = ans.size();
temp.clear();
for(int i = 0; i < length; i++){
if(i % 2 == 0){
temp.push_back(ans[i] * 2 + 0);
temp.push_back(ans[i] * 2 + 1);
}
else{
temp.push_back(ans[i] * 2 + 1);
temp.push_back(ans[i] * 2 + 0);
}
}
ans = temp;
}
return ans;
}
};
119
LeetCode OJ 89. Gray Code的更多相关文章
- 【一天一道LeetCode】#89. Gray Code
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...
- 【LeetCode】89. Gray Code 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】89. Gray Code
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
- 【LeetCode】89. Gray Code (2 solutions)
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
- LeetCode OJ:Gray Code(格林码)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 89. Gray Code - LeetCode
Question 89. Gray Code Solution 思路: n = 0 0 n = 1 0 1 n = 2 00 01 10 11 n = 3 000 001 010 011 100 10 ...
- [LeetCode] 89. Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- leetCode 89.Gray Code (格雷码) 解题思路和方法
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 89. Gray Code
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
随机推荐
- vue 动态路由 Get传值
main.js //2.配置路由 注意:名字 const routes = [ { path: '/home', component: Home }, { path: '/news', compone ...
- typescript泛型类 泛型方法
/* 泛型:软件工程中,我们不仅要创建一致的定义良好的API,同时也要考虑可重用性. 组件不仅能够支持当前的数据类型,同时也能支持未来的数据类型,这在创建大型系统时为你提供了十分灵活的功能. 在像C# ...
- Mysql 之权限体系
1,MySQL权限体系 MySQL 的权限体系大致分为5个层级: 全局层级: 全局权限适用于一个给定服务器中的所有数据库.这些权限存储在mysql.user表中.GRANT ALL ON .和REVO ...
- 内存大小设置 Java heap space错误
1. 问题描述 当从数据库中查询大量的数据,每个模板取出来几百万条数据,或者是频繁的刷新项目.模板时就会占用Java虚拟机JVM的大量内存,超过内存就会出现报java.lang.OutOfMemory ...
- HTML5和CSS3的一些学习记录
1.引述块级文本的标签(blockquote): <blockquote cite="http://www.marktwainbooks.edu/"> <p> ...
- Python三级菜单增删改查
#主要知识点是,字典,列表是使用menu = {'北京':{ '朝阳':{ '国贸':{ 'CICC':{}, 'HP':{}, '渣打银行':{}, 'CCTV':{} }, '望京':{ '陌陌' ...
- Linux 文件,目录,压缩,解压缩操作
2018/11/20 1.find -name 'pom.xml' | xargs perl -pi -e 's|oldString|newString|g' (批量替换) ( 命令行中使用Perl ...
- JAVA 操作mysql 存储 调用
HashMap map = new HashMap(); map.put("a", 9); map.put("b", ""); List&l ...
- 流程图工具Visual Paradigm for UML
- 学习笔记:Highcharts
(Highcharts 167K: ECharts 354K: jqChart 240K),如果用于网络,Highchart最小 Highcharts 功能强大.开源.美观.图表丰富.兼容绝大多数浏 ...