【leetcode】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.


题解:查了资料才发现,原来可以从二进制码转成gray码。比如说二进制码1101,要转成gray码,过程如下:

(1)左边第一位保留:1

(2)第二位与第一位异或得到第二位:1^1 = 0

(3)第三位与第二位异或得到第三位:0^1 = 1

(4)第四位与第三位异或得到第四位:1^0 = 1

所以最后得到的gray码是1011

有一个简单的公式,二进制码x对应的gray码为(x>>1)^x,上情况就是(1101>>1)^1101=1011,原理就是上述描述的过程,把x右移一位,然后错位相异或。至于第一位,如果原来是1,那么右移后得到的0和这个1相异或得到的还是1;如果原来是0,那么右移后得到的0与这个0相异或得到的还是0.

代码如下:

 1 class Solution {
 2 public:
 3     vector<int> grayCode(int n) {
 4         int total_number = 1 << n;
 5         vector<int> answer;
 6         
 7         for(int i = 0;i < total_number;i++){
 8             answer.push_back((i>>1)^i);
 9         }
10         return answer;
11     }
12 };

 不过我觉的这道题如果变一下,变成找出所有的解,就不能用上述方法了,我们可以用深度优先搜索+剪枝的方法。具体方法就是:以n=2为例,第一层为00,那么第二层有两个元素01和10,从01往下走,第三层有11和00,因为00已经出现过了(可以用一个boolean数组保存是否出现),所以这条路径就被减掉了;从11往下走,有01和10,因为01也出现过了,所以这条路径也被减掉了,最后得到的路径就是00-01-11-10;另外一条00-10-11-01也是一样的。

以下是这个思想的JAVA代码,提交[0,2,3,1]居然被报wrong answer,我也是醉了,看样子对错就没法判断了。先放着吧,欢迎大神指正。

 1 import java.util.ArrayList;
 2 import java.util.List;
 3 
 4 public class Solution {
 5      public static void main(String args[]){
 6         Solution s = new Solution();
 7         List<Integer> a = s.grayCode(1);
 8         System.out.println(a.size());
 9         System.out.println(a);
10     }
11     List<List<Integer>> answer = new ArrayList<List<Integer>>();
12     public List<Integer> grayCode(int n) {
13         if(n ==0){
14             List<Integer> temp = new ArrayList<Integer>();
15             return temp;
16         }
17         int total = (int)Math.pow(2, n);
18         boolean[] has = new boolean[total];
19         StringBuffer code = new StringBuffer();
20         List<Integer> res = new ArrayList<Integer>();
21         res.add(0);
22         has[0] = true;
23         for(int i = 0;i < n;i++)
24             code.append("0");
25         grayCodeDFS(res,has, code, n, 2);
26         
27         return answer.get(0);
28     }
29     
30     private void grayCodeDFS(List<Integer> res,boolean[] has,StringBuffer code,int n,int level){
31         if(level == (int)Math.pow(2, n)+1){
32             List<Integer> templist = new ArrayList<Integer>();
33             for(int i = 0;i < res.size();i++)
34                 templist.add(res.get(i));
35             answer.add(templist);
36         }
37         for(int i = 0;i < n;i++){
38             code.setCharAt(i, code.charAt(i)=='1'?'0':'1');
39             int codeNum = Integer.parseInt(code.toString(),2);
40             if(has[codeNum] != true){
41                 res.add(codeNum);
42                 has[codeNum] = true;
43                 grayCodeDFS(res,has, code, n, level+1);                
44                 has[codeNum] = false;
45                 res.remove(res.size()-1);
46             }
47             code.setCharAt(i, code.charAt(i)=='1'?'0':'1');
48         }
49     }
50 }

 

 

posted @ 2014-06-10 11:22  SunshineAtNoon  阅读(162)  评论(0编辑  收藏  举报