zoukankan      html  css  js  c++  java
  • [LeetCode] Gray Code

    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.

    Hide Tags
     Backtracking
     
     
    思路一:

    格雷码 (Gray Code) 的定义请参考 http://en.wikipedia.org/wiki/Gray_code。
    自然二进制码转换为格雷码:g 0 = b 0 , g i = b i  b i − 1
    保留自然二进制码的最高位作为格雷码的最高位,格雷码次高位为二进制码的高位与次高位异
    或,其余各位与次高位的求法类似。例如,将自然二进制码 1001,转换为格雷码的过程是:保留最
    高位;然后将第 1 位的 1 和第 2 位的 0 异或,得到 1,作为格雷码的第 2 位;将第 2 位的 0 和第 3 位
    的 0 异或,得到 0,作为格雷码的第 3 位;将第 3 位的 0 和第 4 位的 1 异或,得到 1,作为格雷码的
    第 4 位,最终,格雷码为 1101。
    格雷码转换为自然二进制码:b 0 = g 0 , b i = g i  b i − 1
    保留格雷码的最高位作为自然二进制码的最高位,次高位为自然二进制高位与格雷码次高位异
    或,其余各位与次高位的求法类似。例如,将格雷码 1000 转换为自然二进制码的过程是:保留最高
    位 1,作为自然二进制码的最高位;然后将自然二进制码的第 1 位 1 和格雷码的第 2 位 0 异或,得
    到 1,作为自然二进制码的第 2 位;将自然二进制码的第 2 位 1 和格雷码的第 3 位 0 异或,得到 1,
    作为自然二进制码的第 3 位;将自然二进制码的第 3 位 1 和格雷码的第 4 位 0 异或,得到 1,作为
    自然二进制码的第 4 位,最终,自然二进制码为 1111。
    格雷码有数学公式,整数 n 的格雷码是 n ^ (n/2), 也就是n^(n>>1)。
    这题要求生成 n 比特的所有格雷码。

    class Solution {
        public:
            int num2Gray(int num)
            {   
                return num ^ (num >> 1); 
            }   
            vector<int> grayCode(int n)  
            {   
                vector<int> res ;
    
                int size = 1 << n;// 2^n
    
                for(int i = 0; i < size; i++)
                {   
                    res.push_back(num2Gray(i));
                }   
    
                return res;
            }   
    };

    思路 2,n 比特的格雷码,可以递归地从 n   1 比特的格雷码生成。如图 §2-5所示。 也就是backtrace???不知道为什么其tag是时backtrace

     

    class Solution {
        public:
            vector<int> grayCode(int n)
            {
                vector<int> res ;
    
                res.push_back(0);
           
    
                for(int i = 0; i < n; i++)
                {
                    int size = res.size();
                    for(int j = size -1; j >= 0; j--)
                    {
                        res.push_back(res[j] + (1 << i));
                    }
                }
    
    
                return res;
            }
    };
  • 相关阅读:
    git 获取之前某个版本
    mysql默认查询顺序
    tp5链式查询fetchSql(true)方法
    微信中关闭网页输入内容时的安全提示
    SourceTree + BeyondCompare 配置 + 使用教程
    SourceTree 免登录跳过初始设置
    git 常规发布流程
    Git常用操作命令
    手动安装phpRedisAdmin
    docker-compose快速搭建lnmp+redis服务器环境
  • 原文地址:https://www.cnblogs.com/diegodu/p/4371807.html
Copyright © 2011-2022 走看看