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. cols

    题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵中 ...

  2. 有关Kali的方法

    Kali 找回系统登陆密码的方式:https://jingyan.baidu.com/article/47a29f24560e77c0142399e3.html

  3. 《Scrum实战》第0次课【如何学习敏捷】全团课后任务汇总

    <Scrum实战>第0次课作业 完成情况: 课程名称:如何学习敏捷 1组 孟帅 孟帅: http://www.cnblogs.com/mengshuai1982/p/7096338.htm ...

  4. day02 Python 的模块,运算,数据类型以及方法

    初识pyhton的模块: 什么是模块: 我的理解就是实现一个功能的函数,把它封装起来,在你需要使用的时候直接调用即可,我的印象里类似于shell 的单独函数脚本. python 的模块分为标准的和第三 ...

  5. Leetcode26--->Remove Duplicates from Sorted Array(从排序数组中移除相同的元素)

    题目: 给定一个排序数组,移除重复出现的元素,保证每个元素最终在数组中只出现一次.返回新数组的长度length; 要求:不能分配额外的一个数组使用,必须使用原地排序的思想,且空间复杂度为O(1) 举例 ...

  6. 设计模式之单例模式 Singleton

    核心作用 保证一个类只有一个实例,并且提供一个访问该实例的全局访问点. 常见应用场景 优点 由于单例模式只生成一个实例,减少了系统性开销,当一个对象的产生需要比较多的资源时,如读取配置,产生其他依赖对 ...

  7. [linux time命令学习篇] time 统计命令执行的时间

    注意: 命令后面一定要有分号; http://codingstandards.iteye.com/blog/798788 用途说明 time命令常用于测量一个命令的运行时间,注意不是用来显示和修改系统 ...

  8. TOJ1196: RSA Signing

    题不在多,大概是多了也可能就是菜逼 1196: RSA Signing Time Limit(Common/Java):3000MS/30000MS     Memory Limit:65536KBy ...

  9. Map容器——TreeMap及常用API,Comparator和Comparable接口

    TreeMap及常用API ①   TreeMap类通过使用红黑树实现Map接口; ②   TreeMap提供按排序顺序存储键/值对的有效手段,同时允许快速检索; ③   不像散列(HashMap), ...

  10. POJ 1145 Tree Summing

    Tree Summing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7698   Accepted: 1737 Desc ...