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 ...
随机推荐
- TF:Tensorflow结构简单应用,随机生成100个数,利用Tensorflow训练使其逼近已知线性直线的效率和截距—Jason niu
import os os.environ[' import tensorflow as tf import numpy as np x_data = np.random.rand(100).astyp ...
- Linux学习之进程管理(十九)
Linux学习之进程管理 进程查看 查看系统中所有进程,使用BSD操作系统的格式 语法:ps aux 选项: a:显示所有前台进程 x:显示所有后台进程 u:显示这个进程是由哪个用户产生的 语法:ps ...
- 服务端线程模型-NIO服务模型
上接<服务端线程模型-线程池服务模型>(https://www.cnblogs.com/fudashi233/p/10549221.html). 这篇分享从最初的单线程服务模型一直演进到线 ...
- javascript事件绑定和普通事件的区别
<!doctype html><html lang="en"><head> <meta charset="UTF-8" ...
- Max Factor 2710 最大的合数的质数因子
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2710 思路:用类似“埃氏筛法”求素数的方法 只是不在把合数标记为1 而是标记为他是因子数. 最后比较大小即 ...
- 纯js上传文件 很好用
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- C++程序设计方法4:类模板
类模板 在定义类时也可以将一些类型抽象出来,用模板参数来替换,从而使类更具有通用性.这种类被称为模板类,例如: template <typename T> class A { T data ...
- Android Studio 2.3 解决小米手机调试安装apk失败问题
在开发者选项里面,拉到底,把miui优化选项去掉就好了. 参考资料 [问题反馈] Android Studio 2.3 在红米note3手机中 调试安装Apk失败
- oracle 连接
1.简述 1) 两个表的连接,是通过将一个表中的一列或者多列同另一个表中的列链接而建立起来的.用来连接两张表的表达式组成了连接条件.当连接成功后,第二张表中的数据就同第一张表连接起来了,并形成了复合 ...
- python之多态与多态性
1.多态的概念:多态指的是一类事物有多种形态,(一个抽象类有多个子类,因而多态的的概念依赖于继承) 比如:序列类型有多种形态:字符串,列表,元组 动物有多种形态:人,狗,猪 import abc cl ...