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 ...
随机推荐
- 046 Oracle执行慢的SQL
-- 执行最慢的sql SELECT * FROM (SELECT sa.SQL_TEXT, sa.SQL_FULLTEXT, sa.EXECUTIONS as "exeCount" ...
- 025 SSM简单搭建
参考了同事的文档,自己也写一篇文档. 同时,补充了一下,程序是如何运行的. 一:SSM框架 1.说明 SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC ...
- SpringMVC-1-(简介及HelloWord)
首先我们来看一下servlet的处理请求的方式: 一:SpringMVC简介: 一)SpringMVC中的几个重要组件 1.DispatchServlet: 前端控制器,mvc模式中的c,是整个流程的 ...
- python3 在 windows 读取路径多了一个\u202a 是咋回
python3 在 windows 读取路径多了一个\u202a 是咋回事
- python + seleinum +phantomjs 设置headers和proxy代理
python + seleinum +phantomjs 设置headers和proxy代理 最近因为工作需要使用selenium+phantomjs无头浏览器,其中遇到了一些坑,记录一下,尤 ...
- 解决UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range
字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(en ...
- 【C#】 break continue return 的区别
static void Main(string[] args) { Console.WriteLine("使用break退出循环"); ; i < ; i++) { ) { ...
- format 用法
hon2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱.语法 它通过{}和:来代替% ...
- 洛谷P2569 股票交易
题目传送门https://www.luogu.org/problemnew/show/P2569 第一眼看题就觉得是个dp ,然后看到2000的范围,hmm大概是个n^2的2维dp 开始设状态,第一维 ...
- Spring cloud Eureka错误锦集(二)
最近学习spring cloud,在测试Eureka作为服务注册中心的时候碰到了问题,错误提示如下: "D:\Program\Java\JDK1.8\bin\java" -XX:T ...