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 ...
随机推荐
- UBuntu14.04 --vim安装YouCompleteMe插件
说明 我电脑的系统参数(用 uname -a命令查看)如下: Linux avyn-Lenovo --generic #-Ubuntu SMP Tue Mar :: UTC i686 i686 i68 ...
- bind1st bind2nd的使用
STL中的函数 bind1st. bind2nd用于将一个二元算子转换成一元算子,需要两个 参数,要转换的bf和一个值v. 参考:http://blog.csdn.net/simahao/articl ...
- EasyUi – 1.入门
1.页面引用. jquery,easyui,主题easyui.css,图标ico.css,语言zh_CN.js <script src="Scripts/jquery-easyui-1 ...
- MVC公开课 – 2.查询,删除 (2013-3-15广州传智MVC公开课)
查询 /Controller/HomeController.cs /// <summary> /// 查询 文章 列表 /// </summary> /// <retur ...
- phpmailer发送邮件出现错误:stream_socket_enable_crypto(): SSL operation failed with code 1.
如果开了调试,调试进去会看到错误提示: smtp_code:"stream_socket_enable_crypto(): SSL operation failed with code 1. ...
- JS 数据类型转换以其他
JavaScript 是一种弱类型的语言,也就是没有类型限制,变量可以随时被赋予任意值. 同时,在程序运行过程中,类型会被自动确认的.因此,这就是涉及到数据的类型转换.在 JS 的世界中,数据类型转换 ...
- Wannafly挑战赛18 B - 随机数
思路:化简公式,Pn 表示 进行n 次操作,有奇数次1的概率 Pn = (1 - x) * Pn - 1 + x * (1 - Pn - 1) 得通项公式 Pn = (1 - (1 - 2 * x) ...
- 微软企业库5.0 学习之路——第十步、使用Unity解耦你的系统—PART2——了解Unity的使用方法(3)
今天继续介绍Unity,在上一篇的文章中,我介绍了使用UnityContainer来注册对象之间的关系.注册已存在的对象之间的关系,同时着重介绍 了Unity内置的各种生命周期管理器的使用方法,今天则 ...
- 十三oracle --控制结构(分支,循环,控制)
.使用各种if语句2.使用循环语句3.使用控制语句——goto和null(goto语句不推荐使用): 二.条件分支语句pl/sql中提供了三种条件分支语句if—then,if–then–else,if ...
- openldap quick start guide
openldap 2.4 在centos 7 x64系统上部署 1 下载源码编译解压tar -xvf xx ./configure make && make install 2 更改配 ...