700块以下,是Pentax的KX最好吗?# PhotoGear - 摄影器材
w*x
1 楼
做了无数recursion的题后花了两小时才整出gray code解, 大家鄙视我吧, 哈哈哈:
/*
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 [0,1,3,2]. Its gray code sequence is:
00 - 0
01 - 1
11 - 3
10 - 2
*/
void _inner_sol(int& nCur, vector& vec, int nStopPos, int nPos)
{
if (nPos < 0)
{
vec.push_back(nCur);
return;
}
_inner_sol(nCur, vec, nStopPos, nPos-1);
if (nPos >= nStopPos)
return;
//flip nPos at nCur
nCur ^= (1 << nPos);
_inner_sol(nCur, vec, nStopPos, nPos-1);
}
vector grayCode(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector vec;
if (0 == n)
vec.push_back(0);
else
{
int nCur = 0;
_inner_sol(nCur, vec, n, 31);
}
return vec;
}
/*
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 [0,1,3,2]. Its gray code sequence is:
00 - 0
01 - 1
11 - 3
10 - 2
*/
void _inner_sol(int& nCur, vector
{
if (nPos < 0)
{
vec.push_back(nCur);
return;
}
_inner_sol(nCur, vec, nStopPos, nPos-1);
if (nPos >= nStopPos)
return;
//flip nPos at nCur
nCur ^= (1 << nPos);
_inner_sol(nCur, vec, nStopPos, nPos-1);
}
vector
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector
if (0 == n)
vec.push_back(0);
else
{
int nCur = 0;
_inner_sol(nCur, vec, n, 31);
}
return vec;
}