/*
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;
}
}
89. Gray Code
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來再榄,“玉大人,你說我怎么就攤上這事享潜±福” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵剑按,是天一觀的道長疾就。 經(jīng)常有香客問我,道長艺蝴,這世上最難降的妖魔是什么猬腰? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮猜敢,結(jié)果婚禮上姑荷,老公的妹妹穿的比我還像新娘。我一直安慰自己缩擂,他們只是感情好鼠冕,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著胯盯,像睡著了一般懈费。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上博脑,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼乡数!你這毒婦竟也來了椭蹄?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬榮一對情侶失蹤净赴,失蹤者是張志新(化名)和其女友劉穎绳矩,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體玖翅,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡翼馆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年割以,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片应媚。...
- 正文 年R本政府宣布丢胚,位于F島的核電站翩瓜,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏携龟。R本人自食惡果不足惜兔跌,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望峡蟋。 院中可真熱鬧坟桅,春花似錦、人聲如沸层亿。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽匿又。三九已至方灾,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間碌更,已是汗流浹背裕偿。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長得像旭绒,于是被迫代替她去往敵國和親鸟妙。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 一挥吵、題目 二重父、解題 Gray Code:格雷碼 題目的意思是在給出一個(gè)字節(jié)長度n,生成一個(gè)序列忽匈,這個(gè)序列的要求是格...