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. mysql5.6做单向主从复制Replication

    原理场景:MySQL从3.23版本开始提供复制功能.指的是将主数据库的DDL和DML操作通过二进制日志传到从服务器(也叫从库),然后在从库上对这些日志重新执行, 从而使得从库和主库的数据保持同步. 优 ...

  2. Ubuntu常用软件安装(小集合)

    跨平台系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#linux Linux包系列的知识:https://www.cnblogs.com/dun ...

  3. BZOJ--1045-- 糖果传递(中位数,排序)

    题目链接 :BZOJ--1045-- 糖果传递 我们知道如果不头尾相连的话 直接求一个前缀和 答案为ans+=s[i] 不相连的话就是1 和n之间断开 头尾相连的话就是 在第k个人之间断开 设A[i] ...

  4. FastDFS 文件上传工具类

    FastDFS文件上传工具类 import org.csource.common.NameValuePair; import org.csource.fastdfs.ClientGlobal; imp ...

  5. NOI 2002 贪吃的九头龙

    树形dp #include<bits/stdc++.h> #define N 305 using namespace std; struct LEB{ int to,nxt,w; }e[N ...

  6. notepad问题汇总

    右键无法设置为默认打开方式:https://blog.csdn.net/jl1134069094/article/details/50749075

  7. 【洛谷P1303 A*B Problem】

    题目描述 求两数的积. 输入输出格式 输入格式: 两行,两个数. 输出格式: 积 输入输出样例 输入样例#1: 1 2 输出样例#1: 2 说明 每个数字不超过10^2000,需用高精 emm,显然本 ...

  8. 如何测试连接MsSQL数据库-------UDL文件

    http://www.xinnet.com/service/cjwt/idc/sjk/1360.html 如果您所使用的 SQL Server 数据库连不上,可以通过这个方法进行测试数据库连接. 温馨 ...

  9. tail 命令只查看日志中的关键字所在行信息

    tail -f info_log-2019-04-20.log |grep 要查询的关键字

  10. 异步ztree 加复选框 及相应后台处理

    异步加载 tree,点一下节点,就发一下请求到后台,然后显示出得到的当前层级节点 <!DOCTYPE html> <html> <head> <meta ch ...