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. 【AC自动机】bzoj4327: JSOI2012 玄武密码

    题目思路没话讲:主要是做题时候的细节和经验 Description 在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香河中 ...

  2. 分享几个能用的 editplus 注册码

    转载自: https://www.cnblogs.com/shihaiming/p/6422441.html 原文:http://host.zzidc.com/wljc/1286.html EditP ...

  3. 【Kafka】搭建和测试等问题

    1.安装启动kafka #跳转到下载目录cd /opt/setup # 下载安装包 wget http://mirror.bit.edu.cn/apache/kafka/0.10.2.0/kafka_ ...

  4. Ajax原生代码

    Ajax传数据有两种方式:get/post.下面是前台的get/post方式的代码. //------------原生--------- function AjaxGET(){ //第一步 调用Aja ...

  5. pyqt设计

    pyqt是python设计GUI的第三方包 作为一个小白,我觉得这篇博客贼好,我就是按照这个博客写的. 这个博客一共分5步,每一步都特别详细. pyqt 打包exe时遇到的问题(我的python环境是 ...

  6. python里字典的用法介绍

    一.什么是字典 字典是python里的一种数据类型,特点是元素的无序性,和键key的唯一性.字典的创建方法是{key:values},字典里的键key只能是不可变的数据类型(整型,字符串或者是元组), ...

  7. python模块之pickle

    和json不同的是: json只支持str,int,tuple,list,dict. pickle支持python里所有的数据类型,但是只能在python里序列化,不跨平台,python独有. 代码示 ...

  8. The 2018 ACM-ICPC China JiangSu Provincial Programming Contest I. T-shirt

    JSZKC is going to spend his vacation! His vacation has N days. Each day, he can choose a T-shirt to ...

  9. SpringDataJpa错误

    在运行项目的时候出现的错误如下: would dispatch back to the current handler URL [/save] again. Check your ViewResolv ...

  10. 学习boundingRectWithSize:options:attributes:context:计算文本尺寸

    oundingRectWithSize:options:context: 返回文本绘制所占据的矩形空间. - (CGRect)boundingRectWithSize:(CGSize)size opt ...