LeetCode - Gray Code
2013-12-27 02:28
381 查看
Gray Code
2013.12.27
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
Note:
For a given n, a gray code sequence is not uniquely defined.
For example,
For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.
Solution:
I remember first hearing the term "Gray code" in discrete mathematics class. It's a special kind of signaling, with every pair of adjacent codes having only one bit of variation. The Taichi Bagua in ancient Chinese culture is an interesting example of Gray code.
To generate a set of valid gray code, you can follow the process below:
1. You have an empty signal at first, [""]
2. You add 0 and 1 to it, ["0", "1"]
3. You put them together, with the second half in reversed order: ["0", "1", "1", "0"]
4. Add 0 to the first half, and 1 to the second half: ["00", "01", "11", "10"]
Guess you've already figured it out. If A
is a valid Gray code sequence of size n, it would stil be valid sequence if reversed. You just make sure you can put A
and inv(A
) together to create an A[n + 1]. That's how you build a Gray code sequence of any size.
Time complexity is O(2^n), as there're 2^n Gray codes in length n. Space compelxity is O(2^n), as each iteration needs space to hold the intermediate result.
Accepted code:
2013.12.27
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.
Solution:
I remember first hearing the term "Gray code" in discrete mathematics class. It's a special kind of signaling, with every pair of adjacent codes having only one bit of variation. The Taichi Bagua in ancient Chinese culture is an interesting example of Gray code.
To generate a set of valid gray code, you can follow the process below:
1. You have an empty signal at first, [""]
2. You add 0 and 1 to it, ["0", "1"]
3. You put them together, with the second half in reversed order: ["0", "1", "1", "0"]
4. Add 0 to the first half, and 1 to the second half: ["00", "01", "11", "10"]
Guess you've already figured it out. If A
is a valid Gray code sequence of size n, it would stil be valid sequence if reversed. You just make sure you can put A
and inv(A
) together to create an A[n + 1]. That's how you build a Gray code sequence of any size.
Time complexity is O(2^n), as there're 2^n Gray codes in length n. Space compelxity is O(2^n), as each iteration needs space to hold the intermediate result.
Accepted code:
// 1CE 2WA 1AC, you fool.. // Too careless!!! class Solution { public: vector<int> grayCode(int n) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. vector<int> res1, res2; res1.clear(); res2.clear(); if(n < 0){ return res1; } int nn = 1 << n; int i, j; res1.push_back(0); for(i = 1; i <= n; ++i){ nn = 1 << (i - 1); for(j = 0; j < nn; ++j){ res2.push_back(res1[j]); } for(j = nn - 1; j >= 0; --j){ res2.push_back(nn + res1[j]); } res1 = res2; res2.clear(); } return res1; } };
相关文章推荐
- PENULTIMATE WORD解题思路 codeEval
- Sublime Text 3061 增强版 by 赵亮(碧海情天theforever)
- C# 2008 windows mobile 6调试环境
- IE11无法支持Forms认证,,,也就是无法保存COOKIE
- LeetCode - Merge Sorted Array
- 更接近于现实的社交产品设计――社会化广告
- IOS图片压缩和扩展
- roman to integer
- 进程同步之信号量机制(pv操作)及三个经典同步问题
- Power BI的一些视频演示资源
- 张小龙演讲精华:产品经理的方法论与价值观
- Quotient Polynomial
- Power BI的一些视频演示资源
- kernel网址
- 跨越13开启14--猿猿感想
- ViewPager和ScrollView滑动冲突
- 黑马程序员_String类
- 用户访谈过程中遇到的几个失控经历
- Uni2D 入门 -- Atlas
- 黑马程序员--面向对象(二)static关键字、静态代码块、对象的初始化过程