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.

Example 1:

  1. Input: 2
  2. Output: [0,1,3,2]
  3. Explanation:
  4. 00 - 0
  5. 01 - 1
  6. 11 - 3
  7. 10 - 2
  8.  
  9. For a given n, a gray code sequence may not be uniquely defined.
  10. For example, [0,2,3,1] is also a valid gray code sequence.
  11.  
  12. 00 - 0
  13. 10 - 2
  14. 11 - 3
  15. 01 - 1

Example 2:

  1. Input: 0
  2. Output: [0]
  3. Explanation: We define the gray code sequence to begin with 0.
  4.   A gray sequence of n has size = 2n, which for n = 0 the size is 20 = 1.
  5.   Therefore, for n = 0 the gray code sequence is [0].

首先要清楚什么是格雷码,然后用位操作来处理。

Java:

  1. public List<Integer> grayCode(int n) {
  2. List<Integer> result = new LinkedList<>();
  3. for (int i = 0; i < 1<<n; i++) result.add(i ^ i>>1);
  4. return result;
  5. }

Java:

  1. public List<Integer> grayCode(int n) {
  2. List<Integer> rs=new ArrayList<Integer>();
  3. rs.add(0);
  4. for(int i=0;i<n;i++){
  5. int size=rs.size();
  6. for(int k=size-1;k>=0;k--)
  7. rs.add(rs.get(k) | 1<<i);
  8. }
  9. return rs;
  10. }    

Python:

  1. class Solution(object):
  2. def grayCode(self, n):
  3. """
  4. :type n: int
  5. :rtype: List[int]
  6. """
  7. result = [0]
  8. for i in xrange(n):
  9. for n in reversed(result):
  10. result.append(1 << i | n)
  11. return result

Python:

  1. # Proof of closed form formula could be found here:
  2. # http://math.stackexchange.com/questions/425894/proof-of-closed-form-formula-to-convert-a-binary-number-to-its-gray-code
  3. class Solution2(object):
  4. def grayCode(self, n):
  5. """
  6. :type n: int
  7. :rtype: List[int]
  8. """
  9. return [i >> 1 ^ i for i in xrange(1 << n)]

C++:

  1. // Time: (2^n)
  2. // Space: O(1)
  3. class Solution {
  4. public:
  5. vector<int> grayCode(int n) {
  6. vector<int> result = {0};
  7. for (int i = 0; i < n; ++i) {
  8. for (int j = result.size() - 1; j >= 0; --j) {
  9. result.emplace_back(1 << i | result[j]);
  10. }
  11. }
  12. return result;
  13. }
  14. };

C++:  

  1. // Time: (2^n)
  2. // Space: O(1)
  3. // Proof of closed form formula could be found here:
  4. // http://math.stackexchange.com/questions/425894/proof-of-closed-form-formula-to-convert-a-binary-number-to-its-gray-code
  5. class Solution2 {
  6. public:
  7. vector<int> grayCode(int n) {
  8. vector<int> result;
  9. for (int i = 0; i < 1 << n; ++i) {
  10. result.emplace_back(i >> 1 ^ i);
  11. }
  12. return result;
  13. }
  14. };

  

    

All LeetCode Questions List 题目汇总

[LeetCode] 89. Gray Code 格雷码的更多相关文章

  1. leetCode 89.Gray Code (格雷码) 解题思路和方法

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

  2. [LeetCode] Gray Code 格雷码

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

  3. gray code 格雷码 递归

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

  4. [LintCode] Gray Code 格雷码

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

  5. Gray Code - 格雷码

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

  6. Leetcode#89 Gray Code

    原题地址 二进制码 -> 格雷码:从最右边起,依次与左边相邻位异或,最左边一位不变.例如: 二进制: 1 0 0 1 1 1 0 |\|\|\|\|\|\| 格雷码: 1 1 0 1 0 0 1 ...

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

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

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

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

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

随机推荐

  1. MERGE引擎 分表后 快速查询所有数据

    MERGE存储引擎把一组MyISAM数据表当做一个逻辑单元来对待,让我们可以同时对他们进行查询.构成一个MERGE数据表结构的各成员MyISAM数据表必须具有完全一样的结构.每一个成员数据表的数据列必 ...

  2. [Codeforces 1242B]0-1 MST

    Description 题库链接 给你一张 \(n\) 个点的完全图,其中有 \(m\) 条边长度为 \(1\),其余全为 \(0\).问你这张图的最小生成树为多少. \(1\leq n\leq 10 ...

  3. P3232 [HNOI2013]游走——无向连通图&&高斯消元

    题意 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机选 择当前顶点的某条边,沿着这条边走到下一个顶点,获得等于这条边的编 ...

  4. ARDUIN人体检测模块

    http://henrysbench.capnfatz.com/henrys-bench/arduino-sensors-and-input/arduino-hc-sr501-motion-senso ...

  5. 2019.12.11 java练习

    class Demo01 { public static void main(String[] args) { //数组求最大值 int[] arr={1,2,3,4,5,6,7,8,9}; int ...

  6. vue-cli3 ios10白屏问题解决思路

    在出现了这个问题之后先不要盲目的去瞎试,根据网上的方法试了个遍也没解决问题 先看报的是什么错,再针对的解决问题 首先出现的报错是 SyntaxError: Unexpected token '*'  ...

  7. Theano入门笔记2:scan函数等

    1.Theano中的scan函数 目前先弱弱的认为:相当于symbolic的for循环吧,或者说计算图上的for循环,也可以用来替代repeat-until. 与scan相比,scan_checkpo ...

  8. 【2019.12.04】SDN上机第6次作业

    实验拓扑 通过图形化界面建立拓扑 先清除网络拓扑 sudo mn -c 生成Python语句 #!/usr/bin/python from mininet.net import Mininet fro ...

  9. js注册表单中实现地区选择效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 再见,Eclipse。

    阅读本文大概需要 5 分钟. 来源:cnblogs.com/ouyida3/p/9901312.html 最近,改用了 IDEA,同事都说我投敌了.当然,这些同事都是和我一样的“老”程序员.不说毕业生 ...