LeetCode OJ--Gray Code **
http://oj.leetcode.com/problems/gray-code/
求格雷码的表示,主要应用递归。
递归生成码表
- 1位格雷码有两个码字
- (n+1)位格雷码中的前2n个码字等于n位格雷码的码字,按顺序书写,加前缀0
- (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 **的更多相关文章
- [LeetCode] 89. Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【LeetCode】Gray Code
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
- 【leetcode】Gray Code (middle)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【题解】【排列组合】【回溯】【Leetcode】Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- leetcode[88] Gray Code
题目:格雷码. 格雷码是从0开始且之后两个相邻码之间只有一个符号不相同,例如000,100,101,111三个相邻之间只有一个二进制不同. 现在给定一个数字n,然后给出格雷码所对应的数字.例如: Fo ...
- 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 ...
- leetCode 89.Gray Code (格雷码) 解题思路和方法
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- LeetCode题目:Gray Code
原题地址:https://leetcode.com/problems/gray-code/ class Solution { public: vector<int> grayCode(in ...
- Leetcode#89 Gray Code
原题地址 二进制码 -> 格雷码:从最右边起,依次与左边相邻位异或,最左边一位不变.例如: 二进制: 1 0 0 1 1 1 0 |\|\|\|\|\|\| 格雷码: 1 1 0 1 0 0 1 ...
- LeetCode OJ 89. Gray Code
题目 The gray code is a binary numeral system where two successive values differ in only one bit. Give ...
随机推荐
- Docker 镜像&仓库 获取及推送镜像
docker查看.删除镜像 docker镜像存储位置: /var/lib/docker 查看docker信息也可以查看保存位置 docker info 1.列出镜像 docker images -aa ...
- linux 下 docker-compose安装
docker和dockers-compose的版本兼容对照 以下是我的服务器的相关信息 linux版本 [root@izbp16fm097gaw3tdaog2wz bin]# cat /proc/ve ...
- jmeter操作mysql数据库
1.导入jdbc的jar包,因为jmeter本身不能直接连接mysql,所以需要导入第三方的jar包,来连接mysql 2.创建数据库连接配置,mysql的url.端口号.账号.密码 在JDBC Co ...
- 使用jmeter做简单的压测(检查点、负载设置、聚合报告)
1.添加断言(检查点) 在需要压测的接口下添加--断言--响应断言,取接口响应中包含有的数据即可 检查点HTTP请求-->断言-->响应断言1.名称.注释2.Apply to//作用于哪里 ...
- Linux 用户管理切换用户su和提取命令sudo-visudu详解
一.su --run a shell with substitute user and group IDs -,-l,--login make the shell a login shell, cle ...
- R-codes-tips
1. 在shell执行R文件 chmod 0755 file.R Rscript file.R 2. 载入数据 data(dune) 3. attach() 将data.frame添加到R的搜索路径 ...
- build_mem_type_table
该函数设置mem_types结构体数组,结构体定义如下: struct mem_type { unsigned int prot_pte; //二级页表属性 unsigned int prot ...
- 制作iso文件
genisoimage -o fusionstor-yi-2017-03-08.iso(镜像名称) -v -cache-inodes -joliet-long -R -J -T -V ZS -c ...
- POJ3216 最小路径覆盖
首先说一下题意,Q个区域,M个任务,每个区域任务可能有多个,然后给你个到各地所需时间的矩阵,每个任务都有开始和持续时间,问最少需要多少工人? 每个工人只能同时执行一个任务. 通过题意,我的瞬间反应就是 ...
- HDU 5047 Sawtooth 高精度
题意: 给出一个\(n(0 \leq n \leq 10^{12})\),问\(n\)个\(M\)形的折线最多可以把平面分成几部分. 分析: 很容易猜出来这种公式一定的关于\(n\)的一个二次多项式. ...