LeetCode(89):格雷编码

Medium!

题目描述:

格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异。

给定一个代表编码总位数的非负整数 n,打印格雷码序列。格雷码序列必须以 0 开头。

例如,给定 n = 2,返回 [0,1,3,2]。其格雷编码是:

00 - 0
01 - 1
11 - 3
10 - 2

说明:

对于给定的 n,其格雷编码的顺序并不唯一。

例如 [0,2,3,1] 也是一个有效的格雷编码顺序。

解题思路:

格雷码是一种循环二进制单位距离码,主要特点是两个相邻数的代码只有一位二进制数不同的编码,格雷码的处理主要是位操作 Bit Operation,LeetCode中关于位操作的题也挺常见,比如 Repeated DNA Sequences 求重复的DNA序列 Single Number 单独的数字, 和  Single Number II 单独的数字之二 等等。三位的格雷码和二进制数如下:

复制代码
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.cnblogs.com/grandyang/p/4315607.html,明白了转换方法后,这道题完全没有难度。

C++解法一:

 1 // Binary to grey code
 2 class Solution {
 3 public:
 4     vector<int> grayCode(int n) {
 5         vector<int> res;
 6         for (int i = 0; i < pow(2,n); ++i) {
 7             res.push_back((i >> 1) ^ i);
 8         }
 9         return res;
10     }
11 };

然后我们来看看其他的解法,参考维基百科上关于格雷码的性质,有一条是说镜面排列(http://zh.wikipedia.org/wiki/%E6%A0%BC%E9%9B%B7%E7%A0%81)的,n位元的格雷码可以从n-1位元的格雷码以上下镜射后加上新位元的方式快速的得到,如下图所示一般。

有了这条性质,我们很容易的写出代码如下。

C++解法二:

 1 // Mirror arrangement
 2 class Solution {
 3 public:
 4     vector<int> grayCode(int n) {
 5         vector<int> res{0};
 6         for (int i = 0; i < n; ++i) {
 7             int size = res.size();
 8             for (int j = size - 1; j >= 0; --j) {
 9                 res.push_back(res[j] | (1 << i));
10             }
11         }
12         return res;
13     }
14 };

维基百科上还有一条格雷码的性质是直接排列(http://zh.wikipedia.org/wiki/%E6%A0%BC%E9%9B%B7%E7%A0%81),以二进制为0值的格雷码为第零项,第一项改变最右边的位元,第二项改变右起第一个为1的位元的左边位元,第三、四项方法同第一、二项,如此反复,即可排列出n个位元的格雷码。根据这条性质也可以写出代码,不过相比前面的略微复杂,代码如下:

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

C++解法三:

 1 // Direct arrangement 
 2 class Solution {
 3 public:
 4     vector<int> grayCode(int n) {
 5         vector<int> res{0};
 6         int len = pow(2, n);
 7         for (int i = 1; i < len; ++i) {
 8             int pre = res.back();
 9             if (i % 2 == 1) {
10                 pre = (pre & (len - 2)) | ((~pre) & 1);
11             } else {
12                 int cnt = 1, t = pre;
13                 while ((t & 1) != 1) {
14                     ++cnt;
15                     t >>= 1;
16                 }
17                 if ((pre & (1 << cnt)) == 0) pre |= (1 << cnt);
18                 else pre &= ~(1 << cnt);
19             }
20             res.push_back(pre);
21         }
22         return res;
23     }
24 };

上面三种解法都需要事先了解格雷码及其性质,假如我们之前并没有接触过格雷码,那么我们其实也可以用比较笨的方法来找出结果,比如下面这种方法用到了一个set来保存已经产生的结果,我们从0开始,遍历其二进制每一位,对其取反,然后看其是否在set中出现过,如果没有,我们将其加入set和结果res中,然后再对这个数的每一位进行遍历,以此类推就可以找出所有的格雷码了。

C++解法四:

 1 class Solution {
 2 public:
 3     vector<int> grayCode(int n) {
 4         vector<int> res;
 5         unordered_set<int> s;
 6         helper(n, s, 0, res);
 7         return res;
 8     }
 9     void helper(int n, unordered_set<int>& s, int out, vector<int>& res) {
10         if (!s.count(out)) {
11             s.insert(out);
12             res.push_back(out);
13         }
14         for (int i = 0; i < n; ++i) {
15             int t = out;
16             if ((t & (1 << i)) == 0) t |= (1 << i);
17             else t &= ~(1 << i);
18             if (s.count(t)) continue;
19             helper(n, s, t, res);
20             break;
21         }
22     }
23 };

既然递归方法可以实现,那么就有对应的迭代的写法,当然需要用stack来辅助。

C++解法五:

 1 class Solution {
 2 public:
 3     vector<int> grayCode(int n) {
 4         vector<int> res{0};
 5         unordered_set<int> s;
 6         stack<int> st;
 7         st.push(0);
 8         s.insert(0);
 9         while (!st.empty()) {
10             int t = st.top(); st.pop();
11             for (int i = 0; i < n; ++i) {
12                 int k = t;
13                 if ((k & (1 << i)) == 0) k |= (1 << i);
14                 else k &= ~(1 << i);
15                 if (s.count(k)) continue;
16                 s.insert(k);
17                 st.push(k);
18                 res.push_back(k);
19                 break;
20             }
21         }
22         return res;
23     }
24 };

猜你喜欢

转载自www.cnblogs.com/ariel-dreamland/p/9159482.html