C++實(shí)現(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)于格雷碼的,猛地一看感覺完全沒接觸過格雷碼,但是看了維基百科后,隱約的感覺原來好像哪門可提到過,哎全還給老師了。這道題如果不了解格雷碼,還真不太好做,幸虧腦補(bǔ)了維基百科,上面說格雷碼是一種循環(huán)二進(jìn)制單位距離碼,主要特點(diǎn)是兩個(gè)相鄰數(shù)的代碼只有一位二進(jìn)制數(shù)不同的編碼,格雷碼的處理主要是位操作 Bit Operation,LeetCode中關(guān)于位操作的題也挺常見,比如 Repeated DNA Sequences 求重復(fù)的DNA序列, Single Number 單獨(dú)的數(shù)字, 和 Single Number II 單獨(dú)的數(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
其實(shí)這道題還有多種解法。首先來看一種最簡(jiǎn)單的,是用到格雷碼和二進(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值的格雷碼為第零項(xiàng),第一項(xiàng)改變最右邊的位元,第二項(xiàng)改變右起第一個(gè)為1的位元的左邊位元,第三、四項(xiàng)方法同第一、二項(xiàng),如此反復(fù),即可排列出n個(gè)位元的格雷碼。根據(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ì),假如我們之前并沒有接觸過格雷碼,那么我們其實(shí)也可以用比較笨的方法來找出結(jié)果,比如下面這種方法用到了一個(gè)set來保存已經(jīng)產(chǎn)生的結(jié)果,我們從0開始,遍歷其二進(jìn)制每一位,對(duì)其取反,然后看其是否在set中出現(xiàn)過,如果沒有,我們將其加入set和結(jié)果res中,然后再對(duì)這個(gè)數(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; } } };
既然遞歸方法可以實(shí)現(xiàn),那么就有對(duì)應(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++實(shí)現(xiàn)LeetCode(89.格雷碼)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)格雷碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++實(shí)現(xiàn)LeetCode(95.獨(dú)一無二的二叉搜索樹之二)
- C++實(shí)現(xiàn)LeetCode(93.復(fù)原IP地址)
- C++實(shí)現(xiàn)LeetCode(91.解碼方法)
- C++實(shí)現(xiàn)LeetCode(137.單獨(dú)的數(shù)字之二)
- C++實(shí)現(xiàn)LeetCode(136.單獨(dú)的數(shù)字)
- C++實(shí)現(xiàn)LeetCode(187.求重復(fù)的DNA序列)
- C++實(shí)現(xiàn)LeetCode(87.攪亂字符串)
- C++實(shí)現(xiàn)LeetCode(86.劃分鏈表)
- C++實(shí)現(xiàn)LeetCode(139.拆分詞句)
相關(guān)文章
C++編譯器無法捕捉到的8種錯(cuò)誤實(shí)例分析
這篇文章主要介紹了C++編譯器無法捕捉到的8種錯(cuò)誤,是深入學(xué)習(xí)C++所必須加以掌握的排錯(cuò)技能,需要的朋友可以參考下2014-09-09C語(yǔ)言大小端字節(jié)序存儲(chǔ)模式深入解讀
我們知道,當(dāng)編譯器執(zhí)行 “創(chuàng)建變量” 這一代碼時(shí),會(huì)在內(nèi)存中開辟空間相應(yīng)的空間來存儲(chǔ)變量值。而對(duì)于整型變量而言,變量值又是以二進(jìn)制補(bǔ)碼的形式存放的2022-09-09c++標(biāo)準(zhǔn)輸入輸出流關(guān)系的前世今生
這篇文章主要給大家介紹了關(guān)于c++標(biāo)準(zhǔn)輸入輸出流關(guān)系的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04C++中構(gòu)造函數(shù)與析構(gòu)函數(shù)的詳解及其作用介紹
這篇文章主要介紹了C++中構(gòu)造函數(shù)與析構(gòu)函數(shù)的詳解及其作用介紹,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Qt實(shí)現(xiàn)小功能之復(fù)雜抽屜效果詳解
在Qt自帶的控件中,也存在抽屜控件:QToolBar。但是,該控件有個(gè)缺點(diǎn):一次只能展開一個(gè)抽屜信息,無法實(shí)現(xiàn)多個(gè)展開。所以本文將自定義實(shí)現(xiàn)復(fù)雜抽屜效果,需要的可以參考一下2022-10-10c語(yǔ)言socket多線程編程限制客戶端連接數(shù)
這篇文章主要介紹了c語(yǔ)言socket多線程編程,可以限制客戶端連接數(shù),大家參考使用吧2013-12-12