LeetCode/solutions/89. Gray Code.md
2019-03-21 14:50:31 +08:00

68 lines
2.0 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [89. Gray Code](https://leetcode.com/problems/gray-code/)
# 思路
## 思路一
就按照题目的意思进行模拟用数组res和一个set来记录已经产生的结果我们从0开始遍历其二进制每一位对其取反
然后看其是否在set中出现过如果没有我们将其加入set和结果res中然后再递归对这个数进行刚刚的操作这样就可以得到所有的格雷码了。
## 思路二
如果对格雷码(可参考[维基百科](https://zh.wikipedia.org/wiki/%E6%A0%BC%E9%9B%B7%E7%A0%81))熟悉的话这题其实就是将二进制转换为格雷码,
举个例子3位的格雷码和二进制如下所示:
```
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
```
参考维基百科及[这篇博客](http://www.omegaxyz.com/2017/11/16/grayandbi/)可很容易地写出代码。
# C++
## 思路一
``` C++
class Solution {
private:
void helper(vector<int> &res, set<int> &s, int code, int n){
s.insert(code);
res.push_back(code);
for(int i = 0; i < n; i++){
int mask = (1 << i);
int t = code;
if((t & mask) == 0) // 第i位是0
t |= mask; // 把第i位变成1
else // 第i位是1
t &= ~mask; // 把第i位变成0
if(s.count(t)) continue;
helper(res, s, t, n);
break;
}
}
public:
vector<int> grayCode(int n) {
vector<int> res;
set<int>s;
helper(res, s, 0, n);
return res;
}
};
```
## 思路二
``` C++
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;
}
};
```