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. bzoj4666 小z的胡话

    题目描述: bz 题解: 乱搞好题哇. 众所周知斐波那契数列是有循环节的. 我们可以搞出在模$10^x$下与所给得数同余的集合,那么在模$10^{x+1}$下,同余集合一定是原集合及循环若干循环节的大 ...

  2. POJ-1426-Find the multiply

    这题深搜广搜都可以做,深搜的做法就是把每个由1 和 0 组成的数字拓展10倍以及拓展10倍+1,然后压入队列. 这样可以走过所有由10组成的数字,且两个方向平行发展(*10  +0和+1). bfs ...

  3. MySQL中CONCAT()的用法

    MySQL中CONCAT()的用法 在日常开发过程中,特别是在书写接口的时候,经常会遇到字符串拼接的情况,比如在返回图片数据时,数据库里往往存储的是相对路径,而接口里一般是存放绝对地址,这就需要字符串 ...

  4. 【js】【vue】获取当前dom层

    多层嵌套,$event.currentTarget 指当前点击层

  5. 21.Yii2.0框架多表关联一对多查询之性能优化--模型的使用

    控制器里 功能: 通过分类,查分类下的所有文章 //关联查询 public function actionRelatesearch(){ //关联查询 //查询方法一(查一行) 一维数组下的值是obj ...

  6. 18/07/2017 R matrix

    矩阵:二维数组,元素拥有相同模式(数值型,字符型或者逻辑型) mymatrix <- matrix (vector, nrow_num_of_rows, ncol_num_of_columns, ...

  7. 爬虫制作入门学习笔记2:[转]python爬虫实例项目大全

    WechatSogou [1]- 微信公众号爬虫.基于搜狗微信搜索的微信公众号爬虫接口,可以扩展成基于搜狗搜索的爬虫,返回结果是列表,每一项均是公众号具体信息字典. DouBanSpider [2]- ...

  8. C#语言入门

    1.基础知识 2.数据类型 3.控制语句 4.

  9. Windows和linux(ubuntu)互传文件简便快捷的方法

    现在很多开发和测试的工作环境都是Linux,但测试后期报告的处理一般都是在Windows下完成的,所以需要把结果拿到Windows下. 如果是同一台PC还好些(windows下安装linux的虚拟机, ...

  10. day01 项目

    项目名称: 编写登陆接口 项目需求:     1 输入用户名密码     2 认证成功后显示欢迎信息     3 输错3次之后锁定,包括下次运行此程序也要锁定,涉及到持久化的问题只能用python 自 ...