89. Gray Code

/*
89. Gray Code
Total Accepted: 57721 Total Submissions: 160293 Difficulty: Medium
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.

Hide Company Tags Amazon
Hide Tags Backtracking

*/
import java.util.*;

public class GrayCode_88 {
    public static void main(String[] args) {
        int n = 2;
        System.out.print(grayCode(n));

        n=3;
        System.out.print(grayCode(n));
    }




    public static List<Integer> grayCode(int n) {
        // ref: https://leetcode.com/discuss/24634/an-accepted-three-line-solution-in-java
        List<Integer> res = new LinkedList<>();
        for (int i = 0; i < 1<<n; i++) 
            res.add(i ^ i>>1);
        return res;
    }

    /*
    The purpose of this function is to convert an unsigned
    binary number to reflected binary Gray code.

    The operator >> is shift right. The operator ^ is exclusive or.
    */
    int binaryToGray( int num)
    {
        return (num >> 1) ^ num;
    }

    /*
    The purpose of this function is to convert a reflected binary
    Gray code number to a binary number.
    */
    int grayToBinary( int num)
    {
        int mask;
        for (mask = num >> 1; mask != 0; mask = mask >> 1)
        {
            num = num ^ mask;
        }
        return num;
    }


    public static List<Integer> grayCode_sol2(int n) {
        // ref: https://leetcode.com/discuss/10068/share-my-solution
        /*
        My idea is to generate the sequence iteratively. For example, when n=3, we can get the result based on n=2. 00,01,11,10 -> (000,001,011,010 ) (110,111,101,100). The middle two numbers only differ at their highest bit, while the rest numbers of part two are exactly symmetric of part one. It is easy to see its correctness. Code is simple:
        */
        List<Integer> res = new ArrayList<>();
        res.add(0);
        for (int i = 0; i < n; i++) {
            int size = res.size();
            for (int k = size -1; k >= 0; k--) {
                res.add(res.get(k) | 1 << i);
                System.out.print(res); // [0, 1][0, 1, 3][0, 1, 3, 2]
            }
        }
        return res;
    
    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末钻洒,一起剝皮案震驚了整個(gè)濱河市奋姿,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌素标,老刑警劉巖称诗,帶你破解...
    沈念sama閱讀 216,919評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異头遭,居然都是意外死亡粪狼,警方通過查閱死者的電腦和手機(jī)退腥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來再榄,“玉大人,你說我怎么就攤上這事享潜±福” “怎么了?”我有些...
    開封第一講書人閱讀 163,316評論 0 353
  • 文/不壞的土叔 我叫張陵剑按,是天一觀的道長疾就。 經(jīng)常有香客問我,道長艺蝴,這世上最難降的妖魔是什么猬腰? 我笑而不...
    開封第一講書人閱讀 58,294評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮猜敢,結(jié)果婚禮上姑荷,老公的妹妹穿的比我還像新娘。我一直安慰自己缩擂,他們只是感情好鼠冕,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,318評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著胯盯,像睡著了一般懈费。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上博脑,一...
    開封第一講書人閱讀 51,245評論 1 299
  • 那天憎乙,我揣著相機(jī)與錄音,去河邊找鬼叉趣。 笑死泞边,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的君账。 我是一名探鬼主播繁堡,決...
    沈念sama閱讀 40,120評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼乡数!你這毒婦竟也來了椭蹄?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,964評論 0 275
  • 序言:老撾萬榮一對情侶失蹤净赴,失蹤者是張志新(化名)和其女友劉穎绳矩,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體玖翅,經(jīng)...
    沈念sama閱讀 45,376評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡翼馆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,592評論 2 333
  • 正文 我和宋清朗相戀三年割以,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片应媚。...
    茶點(diǎn)故事閱讀 39,764評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡严沥,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出中姜,到底是詐尸還是另有隱情消玄,我是刑警寧澤,帶...
    沈念sama閱讀 35,460評論 5 344
  • 正文 年R本政府宣布丢胚,位于F島的核電站翩瓜,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏携龟。R本人自食惡果不足惜兔跌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,070評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望峡蟋。 院中可真熱鬧坟桅,春花似錦、人聲如沸层亿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽匿又。三九已至方灾,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間碌更,已是汗流浹背裕偿。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留痛单,地道東北人嘿棘。 一個(gè)月前我還...
    沈念sama閱讀 47,819評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像旭绒,于是被迫代替她去往敵國和親鸟妙。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,665評論 2 354

推薦閱讀更多精彩內(nèi)容