LeetCode89: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.
解法一
这道题感觉是一个找规律的题目,找到规律后就非常好求解,感觉不是一道回溯的题。
对于n=2,它的结果包含n=1时的结果左边补零,以及逆序遍历n=1时的结果左边补1。
规律例如以下图,列出了n=1,n=2,n=3时的情况。
依据这个规律假设已知n=k的情况,那么n=k+1的结果包含对n=k的结果左边补零,即保存不变。然后逆序遍历n=k的结果左边补1就可以。
runtime:4ms
class Solution {
public:
vector<int> grayCode(int n) {
vector<int> result(1);
for(int i=0;i<n;i++)
{
for(int j=result.size()-1;j>=0;j--)
{
result.push_back((1<<i)+result[j]);
}
}
return result;
}
}。
解法二
解法二就涉及到gray code的数学知识了。要是知道这个数学知识。能够在几分钟之内就解出这道题。
格雷码能够由相应的十进制数求出:grayCode=i^i>>1
runtime:4ms
class Solution {
public:
vector<int> grayCode(int n) {
vector<int> result;
for(int i=0;i<1<<n;i++)
{
result.push_back(i^i>>1);
}
return result;
}
};
LeetCode89:Gray Code的更多相关文章
- Leetcode89. Gray Code格雷编码
给定一个代表编码总位数的非负整数 n,打印其格雷编码序列.格雷编码序列必须以 0 开头. 示例 1: 输入: 2 输出: [0,1,3,2] 解释: 00 - 0 01 - 1 11 - 3 10 - ...
- [Swift]LeetCode89. 格雷编码 | Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [LeetCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【LeetCode】Gray Code
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
- Gray Code
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
- 【leetcode】Gray Code (middle)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [LintCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 44. Decode Ways && Gray Code
Decode Ways A message containing letters from A-Z is being encoded to numbers using the following ma ...
- LeetCode——Gray Code
Description: The gray code is a binary numeral system where two successive values differ in only one ...
随机推荐
- CentOS7.4 安装 oracle12c
安装依赖 yum install -y binutils.x86_64 compat-libcap1.x86_64 gcc.x86_64 gcc-c++.x86_64 glibc.i686 glibc ...
- mysql视图学习总结(转)
一.使用视图的理由是什么?1.安全性.一般是这样做的:创建一个视图,定义好该视图所操作的数据.之后将用户权限与视图绑定.这样的方式是使用到 了一个特性:grant语句可以针对视图进行授予权限.2.查询 ...
- HDU 1686 Oulipo(KMP变形求子串出现数目(可重))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:给两个字符串A,B求出A中出现了几次B(计算重复部分). 解题思路:稍微对kmp()函 ...
- 深入理解 WordPress 数据库中的用户数据 wp_user
WordPress 使用 wp_users 数据表存储用户的主要数据,该数据表结构类似于wp_posts 和 wp_comments 数据表,存储的是需要经常访问的用户数据,该数据表的结构以及该数据表 ...
- 怎么修改chrome浏览器的字体
点击“自定义字体”可以修改字体风格. 如果习惯看微软雅黑的字体,我们可以点击“宋体”进入字体选择,拖动向下可以找到“微软雅黑”的字体,点击“微软雅黑”,然后再点击“完成”即可. 另外如果设置字 ...
- springboot 集合 meshsite3
工程目录 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...
- HadoopMR-Spark-HBase-Hive
 YARN资源调度: 三种 FIFO 大任务独占 一堆小任务独占 capacity 弹性分配 :计算任务较少时候可以利用全部的计算资源,当队列的任务多的时候会按照比例进行资源平衡. 容量保证:保证队 ...
- github 笔记(一)
笔记预留 0. echo "# Try" >> README.md git init git add README.md git commit -m "fir ...
- ubuntu各种软件安装-装机整套系列
首先声明,本人系统ubuntu 14.04.1 LTS, 以下所有软件均安装于该系统. 一. 首先在windows下删除ubuntu,删除方法如下: 1.进入win7,下载个软件MbrFix,放在C: ...
- fastadmin iframe 表单提交之后跳转
controller 对应的那个js文件中添加: define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function($, und ...