89. Gray Code (Bit)
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.
思路:
二进制-->格雷码:如果二进制码字的第i位和i+1位相同,则对应的格雷码的第i位为0,否则为1。最高为之前可以看作还有一位0,即最高位是和0异或。
*格雷码-->二进制:最左边一位不变,从左边第二位起,将每位与左边一位解码后的值异或,作为该位解码后的值。
class Solution {
public:
vector<int> grayCode(int n) {
vector<int> result;
int size = << n;
for (int i =; i< size; i++)
{
result.push_back(getGrayCode(i));
}
return result;
}
int getGrayCode(int binary)
{
return ((binary >> )^binary);
}
};
89. Gray Code (Bit)的更多相关文章
- 89. Gray Code - LeetCode
Question 89. Gray Code Solution 思路: n = 0 0 n = 1 0 1 n = 2 00 01 10 11 n = 3 000 001 010 011 100 10 ...
- 89. Gray Code
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
- 【LeetCode】89. Gray Code
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
- 89. Gray Code(中等,了解啥是 gray code)
知道啥是 gray code 就是收获了. 下面介绍了 gray code 发明的 motivation, 了解动机后就知道啥是 gray code 了. https://zh.wikipedia.o ...
- 【一天一道LeetCode】#89. Gray Code
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...
- 89. Gray Code返回位运算的所有生成值
[抄题]: The gray code is a binary numeral system where two successive values differ in only one bit. G ...
- LeetCode OJ 89. Gray Code
题目 The gray code is a binary numeral system where two successive values differ in only one bit. Give ...
- 89. Gray Code(公式题)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【LeetCode】89. Gray Code (2 solutions)
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
随机推荐
- 更加灵活的编写控制层的方法____结合eval函数
结合EVAL函数,我们可以把API放到测试用例那边去,就可以使用一个定位元素,测试用例可以使用多个API 发现eval里面可以拼接str.那么写成这样更顺眼 eval("self.d ...
- python selenium-7自动发送邮件
https://jingyan.baidu.com/article/647f0115b78f8d7f2148a8e8.html 1.发送HTML格式的邮件 import smtplib from em ...
- 1076 Forwards on Weibo (30 分)
1076 Forwards on Weibo (30 分) Weibo is known as the Chinese version of Twitter. One user on Weibo ma ...
- 比较有意思的原生态js拖拽写法----摘自javascript高级程序设计3
var DragDrop = function () { var dragging = null; var diffX = 0; var diffY = 0; function handleEvent ...
- ORM Nhibernate框架在项目中的配置
在项目中使用 Nhibernet 时,一定要将 配置文件 .xml 编译方式设置为 嵌入式资源,否则在运行项目时就会出现错误. 以下是hibernate.cfg.xml 的配置,在配置中使用的是 M ...
- Executor框架(七)Future 接口、FutureTask类
Future接口介绍 Future 表示异步计算的结果.它提供了检查计算是否完成的方法,以等待计算的完成,并获取计算的结果. Future 一般由 ExecutorService 的submi ...
- uva-10129-欧拉通路
题意:每一个单词的长度最小2,最大1000,单词开头的字母和另外一个单词的末尾一样就可以连接起来,解所有的单词是不是都可以连接起来,没有遗漏的 把每一个单词的第一个字母当成一个结点,最后一个单词也作为 ...
- 折腾了几个小时,分享下zendstudio10的git使用
今天打开zend10,发现新建项目的地方有 from git,from github,就试了试,发现可以导出,也可以commit,但是没办法push. 就百度百度,发现zendstudio10的git ...
- Nginx相关笔记
相关参考: 编译安装测试nginx https://www.cnblogs.com/jimisun/p/8057156.html
- angular 获取当前值