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)的更多相关文章

  1. 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 ...

  2. 89. Gray Code

    题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...

  3. 【LeetCode】89. Gray Code

    题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...

  4. 89. Gray Code(中等,了解啥是 gray code)

    知道啥是 gray code 就是收获了. 下面介绍了 gray code 发明的 motivation, 了解动机后就知道啥是 gray code 了. https://zh.wikipedia.o ...

  5. 【一天一道LeetCode】#89. Gray Code

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...

  6. 89. Gray Code返回位运算的所有生成值

    [抄题]: The gray code is a binary numeral system where two successive values differ in only one bit. G ...

  7. LeetCode OJ 89. Gray Code

    题目 The gray code is a binary numeral system where two successive values differ in only one bit. Give ...

  8. 89. Gray Code(公式题)

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  9. 【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 ...

随机推荐

  1. Lua C++互传结构体实例

    转自:http://bbs.csdn.net/topics/350261649 =====main.cpp======= #include "stdio.h" extern &qu ...

  2. linux 下各个4K区块文件大小测试速度对比 机械硬盘性能 64K性价比收益最高

    机械硬盘,每个区块取三次数最小值为准,带2G RAM缓存卡 4K3.4 MB/秒 8K7.3 MB/秒 16K9.5 MB/秒 32K16.7 MB/秒 64K44.2 MB/秒 128K67.1 M ...

  3. solr .Net端(SolrNet)

    首先 引用SolrNet.dll Microsoft.Practices.ServiceLocation HttpWebAdapters 也可以用.net IDe 中的 nuget下载 solrnet ...

  4. js、C#获取当前url的参数值

    之前很想做一些封装关于获取URL参数值方法,今天简单整理了一下js和后台代码获取url参数值的方法,有什么不好地方,还请大家包涵,代码如下: 1.JS处理URL参数值 <script langu ...

  5. django-template-forloop

    forloop.counter0   # 是每次循环的index 红色的div标签,居然可以这样写. ex:第一次循环的结果 <div class="item active" ...

  6. 什么是 web 开发

    什么是 web 开发     这几天因为工作需要,了解了一下Web development 的技术路线,来源自     en.wikipedia.org/wiki/Web_development    ...

  7. Node + H5 + WebSocket + Koa2 实现简单的多人聊天

    服务器代码  ( 依赖于 koa2,  koa-websocket ) /* 实例化外部依赖 */ let Koa = require("koa2"); let WebSocket ...

  8. leetcode459

    public class Solution { public bool RepeatedSubstringPattern(string s) { var len = s.Length; ) { ret ...

  9. Win10 C盘根目录权限

    cmd管理员运行 icacls c:\ /setintegritylevel M c盘属性,安全,完全控制.

  10. WP8.1 页面跳转,overwrite后退键

    In 8.1 we use the below code to navigate between pages: this.Frame.Navigate(typeof(PivotPage)); In 8 ...