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 ...
随机推荐
- 八皇后问题(DFS)
题目描述: 要在国际象棋棋盘中放八个皇后,使任意两个皇后都不能互相吃,皇后能吃同一行.同一列,同一对角线上(两个方向的对角线)的任意棋子.现在给一个整数n(n<=92),输出前n种的摆法. 输入 ...
- ccf 201712-4 行车路线(Python实现)
一.原题 问题描述 试题编号: 201712-4 试题名称: 行车路线 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 小明和小芳出去乡村玩,小明负责开车,小芳来导航. 小芳将 ...
- Pytorch学习(一)—— 自动求导机制
现在对 CNN 有了一定的了解,同时在 GitHub 上找了几个 examples 来学习,对网络的搭建有了笼统地认识,但是发现有好多基础 pytorch 的知识需要补习,所以慢慢从官网 API进行学 ...
- poj 3187 三角数问题
题意:给你两个数,一个n表示这个三角有多少层,一个sum表示总和 思路: 类似杨辉三角 1 1 1 1 2 1 第n行的第k个数 为 n!/k!(n-k)! 暴力枚举,因 ...
- linux学习-用户的特殊 shell 与 PAM 模块
特殊的 shell, /sbin/nologin 『无法登入』指的是:『这个使用者无法使用 bash 或其他 shell 来登入系统』而已, 并不是说这个账号就无法使用其他的系统资源! 让某个具有 / ...
- 利用PowerDesigner逆向工程导出PDM模型及生成文档
原文:利用PowerDesigner逆向工程导出PDM模型及生成文档 最近需要对老项目进行重构优化,由于项目都是好几年前的,相关设计资料很不全,最基本的数据库设计文档都没有,只能利用PowerDesi ...
- 多实例设置本地IP访问sql server 数据库
我们本地有时候有多个数据库版本(^_^..别说了都是泪),都是为了兼容不同版本的数据而安装的! 最近我们需要用IP来访问,就有了这一段折腾的历程. 上图片为我安装的三个不同的版本,一个为sql ser ...
- [转]mysql Access denied for user 'root'@'localhost' 问题的解决方法
解决方案如下: # /etc/init.d/mysql stop # mysqld_safe --user=mysql --skip-grant-tables --skip-networking &a ...
- LoadRunner 手动关联
步骤: 1.同样的流程录制两遍,保存在不同的项目中, 2.点击Tools >> Compare with Script 3.选择要比对的脚本 3.找辨识码 4.打开辨识码所在网页,查看源代 ...
- TensorFlow——小练习:feed
feed就是喂入数据 使用feed前必须要有占位符作为操作的对象,在运行操作的时候喂入数据. # _*_coding:utf-8_*_ import tensorflow as tf import n ...