mirror of
https://github.com/ShusenTang/LeetCode.git
synced 2024-09-02 14:20:01 +00:00
Create 89. Gray Code.md
This commit is contained in:
parent
0b8ab7293e
commit
f2cdd3f71b
67
solutions/89. Gray Code.md
Normal file
67
solutions/89. Gray Code.md
Normal file
@ -0,0 +1,67 @@
|
||||
# [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;
|
||||
}
|
||||
};
|
||||
```
|
Loading…
Reference in New Issue
Block a user