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 ...
随机推荐
- 使用powershell批量更新git仓库
Get-ChildItem D:\GitHub\NetCore | ForEach-Object -Process{ cd $_.name; git pull; cd ../ }
- React和Vue组件间数据传递demo
一.React (一)父组件向子组件传数据 简单的向下传递参数 /* Parent */ class App extends Component { render() { return ( <d ...
- 使用laravel框架的eloquent\DB模型连接多个数据库
1.配置.env文件 DB_HOST_TRAILER=127.0.0.1DB_PORT_TRAILER=3306DB_DATABASE_TRAILER=htms_trailerDB_USERNAME_ ...
- 牛客网暑期ACM多校训练营(第七场)A Minimum Cost Perfect Matching(找规律)
题意: 给定n, 求一个0~n-1的全排列p, 使得的和最小 分析: 打表发现最优解肯定是和为0的, 然后如果为2的幂就是直接反转即可, 不然的话就要分开从前面到后面逐步拆分, 具体思想模拟一下n = ...
- ModelViewSet的继承关系
- JAVA 消耗 CPU过高排查方法
#找出cpu占用最高的进程top -H#再次确定进程ps aux|grep 17408 #查看进程的线程(tid) ps -mp 17408 -o THREAD,tid,time#将线程转换为十六进制 ...
- 博客笔记(blog notebook)
1. 机器学习 2. NLP 3. code 实际好人 实际坏人 预测百分比 预测好人 \(p_GF^c(s_c\|G)\) \(p_BF^c(s_c\|B)\) \(F^c(s_c)\) 预测坏人 ...
- Django模板导入和替换、以及对数据库的增加、查看
静态文件引入的3中方式:例如对html模板里面对css样式的引入 STATIC_URL = '/static666/'STATICFILES_DIR=[ os.path.join(BASE_DIR,' ...
- [Offer收割]编程练习赛48
题目1 : 折线中点 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定平面上N个点P1, P2, ... PN,将他们按顺序连起来,形成一条折线. 请你求出这条折线的 ...
- JQuery常用的HTML页控制取值、赋值
1,关于tab页签 获取当前页签的属性: var tabsSelect=$("#easytabs").tabs("getSelected"); var titl ...