# [LeetCode] 89. Gray Code 格雷码

An n-bit gray code sequence is a sequence of 2n integers where:

• Every integer is in the inclusive range [0, 2n - 1],
• The first integer is 0,
• An integer appears no more than once in the sequence,
• The binary representation of every pair of adjacent integers differs by exactly one bit, and
• The binary representation of the first and last integers differs by exactly one bit.

Given an integer n, return any valid n-bit gray code sequence.

Example 1:

Input: n = 2
Output: [0,1,3,2]
Explanation:
The binary representation of [0,1,3,2] is [00,01,11,10].
- 00 and 01 differ by one bit
- 01 and 11 differ by one bit
- 11 and 10 differ by one bit
- 10 and 00 differ by one bit
[0,2,3,1] is also a valid gray code sequence, whose binary representation is [00,10,11,01].
- 00 and 10 differ by one bit
- 10 and 11 differ by one bit
- 11 and 01 differ by one bit
- 01 and 00 differ by one bit


Example 2:

Input: n = 1
Output: [0,1]


Constraints:

• 1 <= n <= 16

Int    Grey Code    Binary
0  　　  000        000
1  　　  001        001
2   　 　011        010
3   　 　010        011
4   　 　110        100
5   　 　111        101
6   　 　101        110
7   　　 100        111

// Binary to grey code
class Solution {
public:
vector<int> grayCode(int n) {
vector<int> res;
for (int i = 0; i < pow(2,n); ++i) {
res.push_back((i >> 1) ^ i);
}
return res;
}
};

// Mirror arrangement
class Solution {
public:
vector<int> grayCode(int n) {
vector<int> res{0};
for (int i = 0; i < n; ++i) {
int size = res.size();
for (int j = size - 1; j >= 0; --j) {
res.push_back(res[j] | (1 << i));
}
}
return res;
}
};

0 0 0
0 0 1
0 1 1
0 1 0
1 1 0
1 1 1
1 0 1
1 0 0

// Direct arrangement
class Solution {
public:
vector<int> grayCode(int n) {
vector<int> res{0};
int len = pow(2, n);
for (int i = 1; i < len; ++i) {
int pre = res.back();
if (i % 2 == 1) {
pre = (pre & (len - 2)) | ((~pre) & 1);
} else {
int cnt = 1, t = pre;
while ((t & 1) != 1) {
++cnt;
t >>= 1;
}
if ((pre & (1 << cnt)) == 0) pre |= (1 << cnt);
else pre &= ~(1 << cnt);
}
res.push_back(pre);
}
return res;
}
};

class Solution {
public:
vector<int> grayCode(int n) {
vector<int> res;
unordered_set<int> s;
helper(n, s, 0, res);
return res;
}
void helper(int n, unordered_set<int>& s, int out, vector<int>& res) {
if (!s.count(out)) {
s.insert(out);
res.push_back(out);
}
for (int i = 0; i < n; ++i) {
int t = out;
if ((t & (1 << i)) == 0) t |= (1 << i);
else t &= ~(1 << i);
if (s.count(t)) continue;
helper(n, s, t, res);
break;
}
}
};

class Solution {
public:
vector<int> grayCode(int n) {
vector<int> res{0};
unordered_set<int> s;
stack<int> st;
st.push(0);
s.insert(0);
while (!st.empty()) {
int t = st.top(); st.pop();
for (int i = 0; i < n; ++i) {
int k = t;
if ((k & (1 << i)) == 0) k |= (1 << i);
else k &= ~(1 << i);
if (s.count(k)) continue;
s.insert(k);
st.push(k);
res.push_back(k);
break;
}
}
return res;
}
};

Github 同步地址：

https://github.com/grandyang/leetcode/issues/89

https://leetcode.com/problems/gray-code/

https://leetcode.com/problems/gray-code/discuss/29891/Share-my-solution

https://leetcode.com/problems/gray-code/discuss/29881/An-accepted-three-line-solution-in-JAVA

LeetCode All in One 题目讲解汇总(持续更新中...)

 微信打赏 Venmo 打赏
posted @ 2015-03-05 13:52  Grandyang  阅读(18238)  评论(3编辑  收藏  举报