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, find the sequence of gray code. A gray code sequence must begin with 0 and with cover all 2nintegers.

Notice

For a given n, a gray code sequence is not uniquely defined.

[0,2,3,1] is also a valid gray code sequence according to the above definition.

Have you met this question in a real interview?

Yes
Example

Given n = 2, return [0,1,3,2]. Its gray code sequence is:

  1. 00 - 0
  2. 01 - 1
  3. 11 - 3
  4. 10 - 2
Challenge

O(2n) time.

LeetCode上的原题,请参见我之前的博客Gray Code

解法一:

  1. class Solution {
  2. public:
  3. /**
  4. * @param n a number
  5. * @return Gray code
  6. */
  7. vector<int> grayCode(int n) {
  8. vector<int> res;
  9. for (int i = ; i < pow(, n); ++i) {
  10. res.push_back((i >> ) ^ i);
  11. }
  12. return res;
  13. }
  14. };

解法二:

  1. class Solution {
  2. public:
  3. /**
  4. * @param n a number
  5. * @return Gray code
  6. */
  7. vector<int> grayCode(int n) {
  8. vector<int> res{};
  9. for (int i = ; i < n; ++i) {
  10. int size = res.size();
  11. for (int j = size - ; j >= ; --j) {
  12. res.push_back(res[j] | ( << i));
  13. }
  14. }
  15. return res;
  16. }
  17. };

解法三:

  1. class Solution {
  2. public:
  3. /**
  4. * @param n a number
  5. * @return Gray code
  6. */
  7. vector<int> grayCode(int n) {
  8. vector<int> res{};
  9. int len = pow(, n);
  10. for (int i = ; i < len; ++i) {
  11. int pre = res.back();
  12. if (i % == ) {
  13. pre = (pre & (len - )) | (~pre & );
  14. } else {
  15. int cnt = , t = pre;
  16. while ((t & ) != ) {
  17. ++cnt; t >>= ;
  18. }
  19. if ((pre & ( << cnt)) == ) pre |= ( << cnt);
  20. else pre &= ~( << cnt);
  21. }
  22. res.push_back(pre);
  23. }
  24. return res;
  25. }
  26. };

解法四:

  1. class Solution {
  2. public:
  3. /**
  4. * @param n a number
  5. * @return Gray code
  6. */
  7. vector<int> grayCode(int n) {
  8. vector<int> res{};
  9. unordered_set<int> s;
  10. stack<int> st;
  11. s.insert();
  12. st.push();
  13. while (!st.empty()) {
  14. int t = st.top(); st.pop();
  15. for (int i = ; i < n; ++i) {
  16. int k = t;
  17. if ((k & ( << i)) == ) k |= ( << i);
  18. else k &= ~( << i);
  19. if (s.count(k)) continue;
  20. s.insert(k);
  21. st.push(k);
  22. res.push_back(k);
  23. break;
  24. }
  25. }
  26. return res;
  27. }
  28. };

解法五:

  1. class Solution {
  2. public:
  3. /**
  4. * @param n a number
  5. * @return Gray code
  6. */
  7. vector<int> grayCode(int n) {
  8. vector<int> res;
  9. unordered_set<int> s;
  10. helper(n, s, , res);
  11. return res;
  12. }
  13. void helper(int n, set<int>& s, int out, vector<int>& res) {
  14. if (!s.count(out)) {
  15. s.insert(out);
  16. res.push_back(out);
  17. }
  18. for (int i = ; i < n; ++i) {
  19. int t = out;
  20. if ((t & ( << i)) == ) t |= ( << i);
  21. else t &= ~( << i);
  22. if (s.count(t)) continue;
  23. helper(n, s, t, res);
  24. break;
  25. }
  26. }
  27. };

[LintCode] Gray Code 格雷码的更多相关文章

  1. [LeetCode] Gray Code 格雷码

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

  2. gray code 格雷码 递归

    格雷码 the n-1 bit code, with 0 prepended to each word, followd by the n-1 bit code in reverse order, w ...

  3. [LeetCode] 89. Gray Code 格雷码

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

  4. Gray Code - 格雷码

    基本概念 格雷码是一种准权码,具有一种反射特性和循环特性的单步自补码,它的循环.单步特性消除了随机取数时出现重大误差的可能,它的反射.自补特性使得求反非常方便.格雷码属于可靠性编码,是一种错误最小化的 ...

  5. HDU 5375 Gray code 格雷码(水题)

    题意:给一个二进制数(包含3种符号:'0'  '1'  '?'  ,问号可随意 ),要求将其转成格雷码,给一个序列a,若转成的格雷码第i位为1,则得分+a[i].求填充问号使得得分最多. 思路:如果了 ...

  6. 089 Gray Code 格雷编码

    格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异.给定一个代表编码总位数的非负整数 n,打印格雷码序列.格雷码序列必须以0开头.例如, 给定 n = 2, 返回 [0,1,3 ...

  7. Leetcode89. Gray Code格雷编码

    给定一个代表编码总位数的非负整数 n,打印其格雷编码序列.格雷编码序列必须以 0 开头. 示例 1: 输入: 2 输出: [0,1,3,2] 解释: 00 - 0 01 - 1 11 - 3 10 - ...

  8. LeetCode:Gray Code(格雷码)

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

  9. 格雷码(Gray code)仿真

    作者:桂. 时间:2018-05-12  16:25:02 链接:http://www.cnblogs.com/xingshansi/p/9029081.html 前言 FIFO中的计数用的是格雷码, ...

随机推荐

  1. C++Primer快速浏览笔记-类型转换

    bool b = 42; // _b is true_ int i = b; // _i has value 1_ i = 3.14; // _i has value 3_ double pi = i ...

  2. 如何安装Ecshop for linux

    下载 http://update.shopex.com.cn/version/program/ECShop/download_ecshop_utf8.php 解压缩之后把upload文件夹中的内容放到 ...

  3. 在 SQL Server 中查询EXCEL 表中的数据遇到的各种问题

    SELECT * FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0','Data Source="D:\KK.xls";User ID=A ...

  4. JavaScript案例一:Window弹窗案例

    注:火狐可运行,谷歌不可运行(安全级别高) <!DOCTYPE html> <html> <head> <title>JavaScript 弹窗案例&l ...

  5. JavaScript内置对象(字符串,数组,日期的处理)

    Date 日期对象 日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒). 定义一个时间对象 : var Udate=new Date(); 注意:使用关键字new,Date()的首 ...

  6. [技术学习]js正则表达式汇总

    一.常用正则表达式关键字 ".":任意字符 "*":任意个数 "+":任意个数,至少一个 "?":0-1个 " ...

  7. S5中新增的Array方法详细说明

      ES5中新增的Array方法详细说明 by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wor ...

  8. zookeeper定时清理log

    在zookeeper的目录下新建一个脚本,内容如下(zookeeper bin下面也有zkCleanup.sh脚本,原理一样,都是调用java类) shell_dir=$(cd ")&quo ...

  9. MongoDB 入门之安装篇

    前言:MongoDB 在各 OS 上的安装比较简单,此文章只用来记录,不考虑技术深度. 一.Ubuntu 导入 MongoDB 公钥,添该软件源文件,更新源列表 sudo apt-key adv -- ...

  10. uva10106(大数乘法)

    public class Product { public static void main(String[] args){ Scanner sc = new Scanner(new Buffered ...