随笔- 509  文章- 0  评论- 151  阅读- 22万 

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 [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[n] 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[n] and inv(A[n]) 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:

复制代码
 1 // 1CE 2WA 1AC, you fool..
 2 // Too careless!!!
 3 class Solution {
 4 public:
 5     vector<int> grayCode(int n) {
 6         // IMPORTANT: Please reset any member data you declared, as
 7         // the same Solution instance will be reused for each test case.
 8         vector<int> res1, res2;
 9         res1.clear();
10         res2.clear();
11         
12         if(n < 0){
13             return res1;
14         }
15         
16         int nn = 1 << n;
17         int i, j;
18         
19         res1.push_back(0);
20         for(i = 1; i <= n; ++i){
21             nn = 1 << (i - 1);
22             for(j = 0; j < nn; ++j){
23                 res2.push_back(res1[j]);
24             }
25             for(j = nn - 1; j >= 0; --j){
26                 res2.push_back(nn + res1[j]);
27             }
28             res1 = res2;
29             res2.clear();
30         }
31         
32         return res1;
33     }
34 };
复制代码

 

 posted on   zhuli19901106  阅读(326)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示