Gray Code
2016-04-28 17:02
465 查看
The gray code is a binary numeral system where two successive values differ in only one bit.
Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.
For example, given n = 2, return
is:
Note:
For a given n, a gray code sequence is not uniquely defined.
For example,
解答:
1.我的想法。
先是加入0,1。 然后对0,1从前往后的后面加0,再对0,1进行从后往前的加1.即 00,10,11,01.
以此类推,再在以上四个里面,先是从前往后挨个在最后加0,再是从后往前挨个加1.
class Solution {
public:
vector<int> grayCode(int n) {
vector<vector<int>> res,vec;
vector<int> v;
v.push_back(0);
if(n==0)
return v;
vec.push_back(v);
v.push_back(1);
if(n == 1)
return v;
v.clear();
v.push_back(1);
vec.push_back(v);
for(int i = 1; i < n; i++){
res.clear();
for(auto& j:vec){
j.push_back(0);
res.push_back(j);
j.pop_back();
}
for(int i = vec.size()-1; i >=0; i--){
vec[i].push_back(1);
res.push_back(vec[i]);
}
vec.clear();
vec.insert(vec.end(),res.begin(),res.end());
}
v.clear();
for(int k = 0; k < res.size(); k++){
int sum = 0;
for(int p = res[k].size()-1; p >=0 ; p--){
sum = sum * 2 +res[k][p];
}
v.push_back(sum);
}
return v;
}
};
2.别人的代码
方法跟我一样,只不过他很巧妙的利用了移位。
vector<int> grayCode(int n) {
vector<int> res;
int bits = 1;
int ntmp=0;
res.push_back(0);
for(int i=0;i<n;i++)
{
bits=1<<i;
for(int j = res.size()-1;j>=0;j--){
ntmp = res.at(j)+bits;
res.push_back(ntmp);
}
}
return res;
}
Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.
For example, given n = 2, return
[0,1,3,2]. Its gray code sequence
is:
00 - 0 01 - 1 11 - 3 10 - 2
Note:
For a given n, a gray code sequence is not uniquely defined.
For example,
[0,2,3,1]is also a valid gray code sequence according to the above definition.
解答:
1.我的想法。
先是加入0,1。 然后对0,1从前往后的后面加0,再对0,1进行从后往前的加1.即 00,10,11,01.
以此类推,再在以上四个里面,先是从前往后挨个在最后加0,再是从后往前挨个加1.
class Solution {
public:
vector<int> grayCode(int n) {
vector<vector<int>> res,vec;
vector<int> v;
v.push_back(0);
if(n==0)
return v;
vec.push_back(v);
v.push_back(1);
if(n == 1)
return v;
v.clear();
v.push_back(1);
vec.push_back(v);
for(int i = 1; i < n; i++){
res.clear();
for(auto& j:vec){
j.push_back(0);
res.push_back(j);
j.pop_back();
}
for(int i = vec.size()-1; i >=0; i--){
vec[i].push_back(1);
res.push_back(vec[i]);
}
vec.clear();
vec.insert(vec.end(),res.begin(),res.end());
}
v.clear();
for(int k = 0; k < res.size(); k++){
int sum = 0;
for(int p = res[k].size()-1; p >=0 ; p--){
sum = sum * 2 +res[k][p];
}
v.push_back(sum);
}
return v;
}
};
2.别人的代码
方法跟我一样,只不过他很巧妙的利用了移位。
vector<int> grayCode(int n) {
vector<int> res;
int bits = 1;
int ntmp=0;
res.push_back(0);
for(int i=0;i<n;i++)
{
bits=1<<i;
for(int j = res.size()-1;j>=0;j--){
ntmp = res.at(j)+bits;
res.push_back(ntmp);
}
}
return res;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性