【leetcode】Gray Code
2014-06-10 11:22
281 查看
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.
题解:查了资料才发现,原来可以从二进制码转成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.
代码如下:
不过我觉的这道题如果变一下,变成找出所有的解,就不能用上述方法了,我们可以用深度优先搜索+剪枝的方法。具体方法就是:以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,我也是醉了,看样子对错就没法判断了。先放着吧,欢迎大神指正。
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.
代码如下:
class Solution { public: vector<int> grayCode(int n) { int total_number = 1 << n; vector<int> answer; for(int i = 0;i < total_number;i++){ answer.push_back((i>>1)^i); } return answer; } };
不过我觉的这道题如果变一下,变成找出所有的解,就不能用上述方法了,我们可以用深度优先搜索+剪枝的方法。具体方法就是:以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,我也是醉了,看样子对错就没法判断了。先放着吧,欢迎大神指正。
import java.util.ArrayList; import java.util.List; public class Solution { public static void main(String args[]){ Solution s = new Solution(); List<Integer> a = s.grayCode(1); System.out.println(a.size()); System.out.println(a); } List<List<Integer>> answer = new ArrayList<List<Integer>>(); public List<Integer> grayCode(int n) { if(n ==0){ List<Integer> temp = new ArrayList<Integer>(); return temp; } int total = (int)Math.pow(2, n); boolean[] has = new boolean[total]; StringBuffer code = new StringBuffer(); List<Integer> res = new ArrayList<Integer>(); res.add(0); has[0] = true; for(int i = 0;i < n;i++) code.append("0"); grayCodeDFS(res,has, code, n, 2); return answer.get(0); } private void grayCodeDFS(List<Integer> res,boolean[] has,StringBuffer code,int n,int level){ if(level == (int)Math.pow(2, n)+1){ List<Integer> templist = new ArrayList<Integer>(); for(int i = 0;i < res.size();i++) templist.add(res.get(i)); answer.add(templist); } for(int i = 0;i < n;i++){ code.setCharAt(i, code.charAt(i)=='1'?'0':'1'); int codeNum = Integer.parseInt(code.toString(),2); if(has[codeNum] != true){ res.add(codeNum); has[codeNum] = true; grayCodeDFS(res,has, code, n, level+1); has[codeNum] = false; res.remove(res.size()-1); } code.setCharAt(i, code.charAt(i)=='1'?'0':'1'); } } }
相关文章推荐
- [LeetCode]Gray Code
- [leetcode] 89.Gray Code
- 算法分析与设计课程(11):【leetcode】Gray Code
- 20170323-leetcode-089-Gray Code
- LeetCode-Gray Code
- 【leetcode】Gray Code
- LeetCode: Gray Code
- [LeetCode][Java] Gray Code
- [leetcode刷题系列]Gray Code
- LeetCode-Gray Code
- leetcode Gray Code 格雷码
- leetcode 89. Gray Code
- [转载]LeetCode: Gray Code
- LeetCode: Gray Code [089]
- leetcode89~Gray Code
- LeetCode | Gray Code
- LeetCode | 89. Gray Code
- [Leetcode] Gray Code
- [Leetcode]Gray Code
- leetcode 日经贴,Cpp code -Gray Code