LeetCode: Gray Code [089]
【题目】
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.
【题意】
本题是有关格雷码转换的,知道二进制转换为格雷码的转换规则,本题就很easy了。
给定二进制下的位数n, 要求得到格雷码序列,并输出每一个格雷码相应的十进制表示。相邻格雷码之间仅仅有一位不同。
对于输入的n,格雷码序列可能有多个,题目要求输出任一个序列就可以。
序列必须以0開始。
【思路】
n位二进制可表示2^n个数。因此格雷码序列长度即为2^n
我们仅仅需从小到大把二进制转换成相应的格雷码就可以。转换规则例如以下:
如果二进制数为x, 则其相应的格雷码为x>>1^x
即x和其本身向右移动一位后的结果做抑或运算。
【注意。n=0是本题觉得它能表示一个值0】
【代码】
class Solution {
public:
vector<int> grayCode(int n) {
vector<int> result;
int size=1<<n; //一共能够表示2^n个数
int x=0;
while(x<size){
result.push_back(x>>1^x); //转换成相应格雷码
x++;
}
return result;
}
};
LeetCode: Gray Code [089]的更多相关文章
- [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 @ Python
原题地址:https://oj.leetcode.com/problems/gray-code/ 题意: The gray code is a binary numeral system where ...
- LeetCode——Gray Code
Description: The gray code is a binary numeral system where two successive values differ in only one ...
- LeetCode:Gray Code(格雷码)
题目链接 The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...
- LeetCode: Gray Code 解题报告
Gray CodeThe gray code is a binary numeral system where two successive values differ in only one bit ...
- [转载]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
题目的意思就是将十进制转换成格雷码 首先将二进制转换成格雷码 根据此图可以看出二进制的第i和第i+1位异或为格雷码的第i+1位,对于给定的十进制数x,其(x>>1)相当于二进制向右移动一位 ...
- [LeetCode]题解(python):089 Gray Code
题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...
- Java for LeetCode 089 Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
随机推荐
- 解决UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position
最近用Python写了些爬虫,在爬取一个gb2312的页面时,抛出异常: UnicodeEncodeError: 'ascii' codec can't encode characters in po ...
- linux抓包工具Charles的配置安装
1. 安装 (不赘述,方法很多,我使用yaourt charles安装) 2. 导出证书,crt格式保存备用 3. 浏览器导入证书 4. 浏览器设置代理
- shell 脚本使用记录
2019-03-26 需求是:因为遇到一些测试偶发性的出现,比如执行了20次会出一次错误,然后就顺手写了一个小脚本,用来判断执行了 n 次,是否出现错误.根据正则来匹配出substring value ...
- 一种表达式语言的解析引擎JEXL简单使用
Jexl 是一个 Expression Language 的解析引擎, 是为了方便嵌入你的系统或者程序框架的开发中, 他算是实现了 JSTL 中 EL 的延伸版本. 不过也采用了一些 Velocity ...
- Linux学习笔记7
执行脚本篇章 r w x 读 写 执行 4 2 1 如果要启动tomcat 服务器 即执行 startup.sh脚本文件 1> #chmod 444 startup.sh 增加文本读权限 -r ...
- 20个命令行工具监控 Linux 系统性能【转载】
对于每个系统管理员或网络管理员来说,每天要监控和调试 Linux 系统性能问题都是非常困难的工作.我已经有5年 Linux 管理员的工作经历,知道如何监控系统使其保持正常运行.为此,我们编写了对于 L ...
- Idea中的Maven
IDEA作为java开发必不可少的一个工具,对于老Eclipse开发人员刚接触的时候可能会有些陌生,同时,maven也是java开发经常使用的工具,有些人可能会不清楚怎么在idea里面使用maven, ...
- JavaScript基础笔记(二)变量、作用域和内存问题
变量.作用域和内存问题 一.基本类型和引用类型的值 基本类型值:简单的数据段 引用类型值:由多个值构成的对象 基本类型是按值访问的,引用类型是按引用访问的. 不能给基本类型的值添加属性,尽管不会报错. ...
- mysql命令(command)
连接mysql命令: -uuserName -pPassword 显示表的索引: SHOW INDDEX FROM table_name 查看mysql的超时时间:SHOW GLOBAL VARIAB ...
- Ruby语法基础(二)
Ruby语法基础(二) 继续ruby的学习,这次主要把目光放到运算符,条件判断,循环,方法,以及其他ruby特有的基本概念上 运算符 算术运算符:+,-,/,%,**,值的注意的是,ruby中一切皆为 ...