Problem:

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:

 -
-
-
-

Solution:

理解格雷码和二进制的转换关系这题就很简单了。

格雷码的特点在于相邻两个数字仅有一位不同。题中给出的示例是【00,01,11,10】,原题提示中也说到格雷码的顺序可能不止一种,如【00,10,11,01】亦可,只需满足相邻两数只有一位不同这一条件。

按题示序列规则,对二进制数进行右移运算和异或运算可以得出相应格雷码,如:

// i ^(i>>1)
()^→()
()^→()
()^→()
()^→()

AC Code(C++):

 class Solution {
public:
vector<int> grayCode(int n) {
vector<int> seq;
for(int i=;i<(<<n);i++){
seq.push_back(i^(i>>));
}
return seq;
}
};

【LeetCode】89.Gary Code的更多相关文章

  1. 【LeetCode】89. Gray Code 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【LeetCode】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 (2 solutions)

    Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...

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

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

  5. 【LeetCode】392. Is Subsequence 解题报告(Python)

    [LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...

  6. 【LeetCode】722. Remove Comments 解题报告(Python)

    [LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...

  7. 【LeetCode】831. Masking Personal Information 解题报告(Python)

    [LeetCode]831. Masking Personal Information 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  8. 【LeetCode】228. Summary Ranges 解题报告(Python)

    [LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...

  9. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

随机推荐

  1. suoi62 网友跳 (暴搜+dp)

    传送门 sbw太神啦orz 首先N<=20可以直接暴搜 然后玄学剪枝可以过18个点 那么N<=40的时候,就把它拆成两半分别暴搜,再用dp拼起来 对于前半段,设f[i][j]是开始高度为i ...

  2. request.getRequestDispatcher 页面跳转,样式丢失。

    在页面中引用样式和其它资源的时候,尽量不要用相对路径,因为"当前路径"这个概念在J2EE中是不稳定的. 所以最好都是绝对路径,类似于: <% String cp = requ ...

  3. 最全面的 Spring 学习笔记

    http://www.codeceo.com/article/learn-spring.html 来源:泊浮目 分享到:更多36 Spring致力于提供一种方法管理你的业务对象.在大量Java EE的 ...

  4. plink:ped格式转换为bed格式

    命令行如下: plink --file FILENAME --make-bed --out FILENAME 第一个FILENAME的后缀为.ped和.map,生成的第二个FILENAME的后缀为.b ...

  5. TestNg 8.参数化测试 - xml

    有的时候,case需要参数,那么,传餐怎么传? 我的目录结构:首先建一个包叫做parameter,然后在resource里面新建一个parameter.xml文件 看以下代码: ParameterTe ...

  6. http的常用方法和状态码

    http常用的方法: 1.get----> 服务器向客户端发送资源,一般来说,是不需要传入参数就可以查看某些信息. 2.post--->客户端向服务器发送请求,一般来说,是传入参数,服务端 ...

  7. function call操作符(operator()) 仿函数(functor)

    主要是需要某种特殊的东西来代表一整组操作 代表一整组操作的当然是函数,过去通过函数指针实现 所以STL算法的特殊版本所接受的所谓条件或策略或一整组操作都以仿函数的形式呈现 #include <i ...

  8. IIS异常:CS0016: 未能写入输出文件“c:\WINDOWS\Microsoft.NET\Framework\.。。”--“拒绝访问

    CS0016: 未能写入输出文件“c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/.........dll” ...

  9. C# 正则表达式中的顺序环视和逆序环视

    环视结构不匹配任何字符,只匹配文本中的特定位置. 顺序环视:从左向右查看文本,尝试匹配子表达式,如果能够匹配则返回匹配成功信息.顺序环视使用「 (?=...) 来标识」,例如「 (?=\d) 」,它表 ...

  10. day08-(xml&&tomcat)

    回顾: jdbc: java语言操作数据库 jdbc是一套规范,oracle公司制定的 驱动:jdbc的实现类,由数据库厂商提供 使用步骤: .导入jar包(驱动) .注册驱动 Class.forNa ...