C++實現(xiàn)LeetCode(89.格雷碼)
[LeetCode] 89.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.
這道題是關(guān)于格雷碼的,猛地一看感覺完全沒接觸過格雷碼,但是看了維基百科后,隱約的感覺原來好像哪門可提到過,哎全還給老師了。這道題如果不了解格雷碼,還真不太好做,幸虧腦補了維基百科,上面說格雷碼是一種循環(huán)二進(jìn)制單位距離碼,主要特點是兩個相鄰數(shù)的代碼只有一位二進(jìn)制數(shù)不同的編碼,格雷碼的處理主要是位操作 Bit Operation,LeetCode中關(guān)于位操作的題也挺常見,比如 Repeated DNA Sequences 求重復(fù)的DNA序列, Single Number 單獨的數(shù)字, 和 Single Number II 單獨的數(shù)字之二 等等。三位的格雷碼和二進(jìn)制數(shù)如下:
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
其實這道題還有多種解法。首先來看一種最簡單的,是用到格雷碼和二進(jìn)制數(shù)之間的相互轉(zhuǎn)化,明白了轉(zhuǎn)換方法后,這道題完全沒有難度,代碼如下:
解法一:
// 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; } };
然后我們來看看其他的解法,參考維基百科上關(guān)于格雷碼的性質(zhì),有一條是說鏡面排列的,n位元的格雷碼可以從n-1位元的格雷碼以上下鏡射后加上新位元的方式快速的得到。
有了這條性質(zhì),我們很容易的寫出代碼如下:
解法二:
// 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; } };
維基百科上還有一條格雷碼的性質(zhì)是直接排列,以二進(jìn)制為0值的格雷碼為第零項,第一項改變最右邊的位元,第二項改變右起第一個為1的位元的左邊位元,第三、四項方法同第一、二項,如此反復(fù),即可排列出n個位元的格雷碼。根據(jù)這條性質(zhì)也可以寫出代碼,不過相比前面的略微復(fù)雜,代碼如下:
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; } };
上面三種解法都需要事先了解格雷碼及其性質(zhì),假如我們之前并沒有接觸過格雷碼,那么我們其實也可以用比較笨的方法來找出結(jié)果,比如下面這種方法用到了一個set來保存已經(jīng)產(chǎn)生的結(jié)果,我們從0開始,遍歷其二進(jìn)制每一位,對其取反,然后看其是否在set中出現(xiàn)過,如果沒有,我們將其加入set和結(jié)果res中,然后再對這個數(shù)的每一位進(jìn)行遍歷,以此類推就可以找出所有的格雷碼了,參見代碼如下:
解法四:
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; } } };
既然遞歸方法可以實現(xiàn),那么就有對應(yīng)的迭代的寫法,當(dāng)然需要用stack來輔助,參見代碼如下:
解法五:
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; } };
到此這篇關(guān)于C++實現(xiàn)LeetCode(89.格雷碼)的文章就介紹到這了,更多相關(guān)C++實現(xiàn)格雷碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c++標(biāo)準(zhǔn)輸入輸出流關(guān)系的前世今生
這篇文章主要給大家介紹了關(guān)于c++標(biāo)準(zhǔn)輸入輸出流關(guān)系的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04C++中構(gòu)造函數(shù)與析構(gòu)函數(shù)的詳解及其作用介紹
這篇文章主要介紹了C++中構(gòu)造函數(shù)與析構(gòu)函數(shù)的詳解及其作用介紹,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09