# int32bit / leetcode

Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
TLE_solve.cpp
solve.cpp

## 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 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.

For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.

## Solution

```vector<int> grayCode(int n) {
vector<int> result;
vector<int> cur;
int N = 1;
for (int i = 0; i < n; ++i)
N <<= 1;
dfs(result, cur, n, 0, N);
return result;
}
void dfs(vector<int> &result, vector<int> cur, int n, int x, int N) {
cur.push_back(x);
if (N == 1) {
result = vector<int>(begin(cur), end(cur));
return;
}
vector<int> next = getPossibleNext(n, x);
for (auto i : next) {
if (find(begin(cur), end(cur), i) == end(cur)) {
dfs(result, cur, n, i, N - 1);
}
}
}
vector<int> getPossibleNext(int n, int x) {
int base = 0x1;
vector<int> result;
for (int i = 0; i < n; ++i) {
result.push_back(base ^ x);
base <<= 1;
}
return result;
}```

```vector<int> grayCode(int n) {
vector<int> result;
for (int i = 0; i < (1 << n); ++i) {
result.push_back(i ^ (i >> 1));
}
return result;
}```

• 把n-1位的结果集，最高位补0，此时n-1位变成了n位，比如0 1 变成00 01, 作为n的结果集。
• 把n-1位的结果集逆序，然后前面补1，作为n的结果集。
• 以上两点合起来就是最后的结果集。

``````00
01
11
10
``````

``````[0]00
[0]01
[0]11
[0]10

[1]10
[1]11
[1]01
[1]00

000
001
011
010
110
111
101
100
``````

```vector<int> grayCode2(int n) {
vector<int> result;
result.push_back(0);
for (int i = 0; i < n; ++i) {
int high = 1 << i;
for (int j = result.size() - 1; j >= 0; --j)
result.push_back(result[j] + high);
}
return result;
}```
You can’t perform that action at this time.