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. C语言中的32个关键字

    C语言中的32个关键字 数据类型关键字(12个) (1)     char:声明字符型变量或函数 (2)     double:声明双精度变量或函数 (3)     enum:声明美剧类型 (4)   ...

  2. STM32的IAP方案

    from:   http://bbs.eeworld.com.cn/thread-294115-1-1.html 几乎所有的同类书籍都介绍综合性的应用示例如“万年历 + 温度显示 + 闹钟响铃 + 计 ...

  3. debian 升级glibc

    原因 wheezy是2.13,编译android4.4 需要2.14的,报错如下: rebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.8-linar ...

  4. CSS效果常见问题

    详细解答参见上篇博客 问题1.如何用 div 画一个 xxx box-shadow 无限投影 (堆叠成复杂图案) ::before ::after 问题2.如何产生不占空间的边框 1.box-shad ...

  5. Win磁盘MBR转换为GUID

    title: Win磁盘MBR转换为GUID date: 2018-09-02 11:52:32 updated: tags: [windows,记录,折腾] description: keyword ...

  6. tarjan - SPFA - Luogu 3387【模板】缩点

    [模板]缩点 题目描述 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只计算一次 ...

  7. python 学习分享-字典篇

    python字典(Dictionary) dict是无序的 key必须是唯一切不可变的 a={'key1':'value1','key2':'value2'} 字典的增删改查 a['key3']='v ...

  8. dict的特性和基本语法——python3.6

    特性 key:value结构,字典中的每一个元素,都是键值对 key必须可被hash,且必须为不可变数据类型,必须唯一 可存放任意多个值,可修改,可以不唯一 无序 查找速度快,因为hash可以把key ...

  9. gdb调试手册 一 gdb概述

    一 gdb概述 gdb调试器的目的是让你了解其他的程序在执行的时候发生了什么或者其他程序崩溃时正在做什么 gdb主要能够在运行中做四类事情(包括这些事情中的一些附加的事情)来帮助你获取bugs a 运 ...

  10. Leetcode with Python

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...