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 ...
随机推荐
- 掌握这些Python代码技巧,编程至少快一半!
被人工智能捧红的 Python 已是一种发展完善且非常多样化的语言,其中肯定有一些你尚未发现的功能.本文或许能够让你学到一些新技巧. Python 是世界上最流行.热门的编程语言之一,原因很多,比 ...
- python2与python3的bytes问题
>>> s = '编程' >>> print s 编程 >>> s '\xe7\xbc\x96\xe7\xa8\x8b' >>> ...
- eclipse使用技巧的网站收集——转载(三)
本文来自:https://www.cnblogs.com/jeffen/p/5965227.html,未经更改,尊重作者 工欲善其事,必先利其器.对于程序员来说,Eclipse便是其中的一个“器”.本 ...
- 线段树:HDU2795-Billboard(建树方式比较新奇)
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- ACM Changchun 2015 L . House Building
Have you ever played the video game Minecraft? This game has been one of the world's most popular ga ...
- Java线程和多线程(四)——主线程中的异常
作为Java的开发者,在运行程序的时候会碰到主线程抛异常的情况.如果开发者使用Java的IDE比如Eclipse或者Intellij IDEA的话,可能是不需要直接面对这个问提的,因为IDE会处理运行 ...
- HDU 3072 SCC Intelligence System
给出一个带权有向图,要使整个图连通.SCC中的点之间花费为0,所以就先缩点,然后缩点后两点之间的权值为最小边的权值,把这些权值累加起来就是答案. #include <iostream> # ...
- 和为n连续正数序列 【微软面试100题 第五十一题】
题目要求: 输入一个正数n,输出所有和为n连续正数序列(至少两个). 例如输入15,由于1+2+3+4+5 = 4+5+6 = 7+8 = 15.所以输出3个连续序列1~5,4~6,7~8. 参考资料 ...
- 深入了解SEO
为什么要SEO,SEO的作用是什么?SEO(Search Engine Optimization)是为了让自己的IT产品优先能被搜索引擎找到,通过搜索引擎搜索推荐给网民浏览(特点就是精准找到用户群体) ...
- day03_08 变量的重新赋值02
python自动回收垃圾内存,不用了自动会回收,但是C不会 以下del代码为手动强拆,就是从内存中删除变量名