http://oj.leetcode.com/problems/gray-code/

求格雷码的表示,主要应用递归。

递归生成码表

这种方法基于格雷码是反射码的事实,利用递归的如下规则来构造:
  1. 1位格雷码有两个码字
  2. (n+1)位格雷码中的前2n个码字等于n位格雷码的码字,按顺序书写,加前缀0
  3. (n+1)位格雷码中的后2n个码字等于n位格雷码的码字,按逆序书写,加前缀1

    #include <iostream>
    #include <vector>
    #include <Cmath>
    using namespace std; class Solution {
    public:
    vector<vector<int> > ans; vector<int> generateGrayCode(int i,int j,int num_bits)
    {
    vector<int> ansTemp;
    if(j == && i == )
    {
    ansTemp.push_back();
    return ansTemp;
    }
    else if(j == && i == )
    {
    ansTemp.push_back();
    return ansTemp;
    } if(i< pow(,(double)num_bits))
    {
    ansTemp = generateGrayCode(i,j-,num_bits-); //顺序
    ansTemp.push_back();
    }
    else
    {
    ansTemp = generateGrayCode( *pow(,(double)num_bits) - i - ,j-,num_bits-); //逆序
    ansTemp.push_back();
    } return ansTemp;
    } vector<int> grayCode(int n) {
    vector<int> answerInt;
    answerInt.clear();
    if(n == )
    {
    answerInt.push_back();
    return answerInt;
    }
    ans.clear();
    vector<int> onePiece;
    for(int i = ;i< pow(,(double)n);i++)
    {
    onePiece = generateGrayCode(i,n-,n-);
    ans.push_back(onePiece);
    }
    int i_ans = ; for(int i = ;i< pow(,(double)n) ;i++)
    {
    onePiece = ans[i];
    //转换成整数
    i_ans = ;
    for(int mm = n-;mm>=;mm--)
    {
    i_ans *=;
    i_ans += onePiece[mm];
    }
    answerInt.push_back(i_ans);
    }
    return answerInt;
    }
    }; int main()
    {
    Solution myS;
    myS.grayCode();
    return ;
    }

LeetCode OJ--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

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

  3. 【leetcode】Gray Code (middle)

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

  4. 【题解】【排列组合】【回溯】【Leetcode】Gray Code

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

  5. leetcode[88] Gray Code

    题目:格雷码. 格雷码是从0开始且之后两个相邻码之间只有一个符号不相同,例如000,100,101,111三个相邻之间只有一个二进制不同. 现在给定一个数字n,然后给出格雷码所对应的数字.例如: Fo ...

  6. Java for LeetCode 089 Gray Code

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

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

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

  8. LeetCode题目:Gray Code

    原题地址:https://leetcode.com/problems/gray-code/ class Solution { public: vector<int> grayCode(in ...

  9. Leetcode#89 Gray Code

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

  10. LeetCode OJ 89. Gray Code

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

随机推荐

  1. Golang 简单静态web服务器

    直接使用 net.http 包,非常方便 // staticWeb package main import ( "fmt" "net/http" "s ...

  2. 15Shell脚本—流程控制

    流程控制语句 尽管可以通过使用Linux命令.管道符.重定向以及条件测试语句编写最基本的Shell脚本,但是这种脚本并不适用于生产环境.原因是它不能根据真实的工作需求来调整具体的执行命令,也不能根据某 ...

  3. MySQL迁移升级解决方案

    任务背景 由于现有业务架构已不能满足当前业务需求,在保证数据完整的前提下,现需要将原有数据库迁移到另外一台单独的服务器上,在保证原有服务正常的情况下,将原有LAMP环境中mysql数据库版本5.6.3 ...

  4. python标准输入输出

    input() 读取键盘输入 input() 函数从标准输入读入一行文本,默认的标准输入是键盘. input 可以接收一个Python表达式作为输入,并将运算结果返回. print()和format( ...

  5. jsp常用动作

    jsp:include 动态包含: jsp:forward 转发: jsp:useBean 实例化bean对象: jsp:setProperty 设置一个属性值 jsp:getProperty 获取一 ...

  6. mvc “System.NullReferenceException”类型的异常在 App_Web_zo44wdaq.dll 中发生,但未在用户代码中进行处理 其他信息: 未将对象引用设置到对象的实例。

    “System.NullReferenceException”类型的异常在 App_Web_zo44wdaq.dll 中发生,但未在用户代码中进行处理 其他信息: 未将对象引用设置到对象的实例. 解决 ...

  7. 数据库导入Exel,输入到浏览器

    db.php <?php require dirname(__FILE__)."/dbconfig.php";//引入配置文件 class db{ public $conn= ...

  8. Leetcode 476.数字的补数

    数字的补数 给定一个正整数,输出它的补数.补数是对该数的二进制表示取反. 注意: 给定的整数保证在32位带符号整数的范围内. 你可以假定二进制数不包含前导零位. 示例 1: 输入: 5 输出: 2 解 ...

  9. C++ POST方式访问网页

    bool PostContent(CString strUrl, const CString &strPara, CString &strContent, CString &s ...

  10. hdu1599 find the mincost route floyd求出最小权值的环

    find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...