Java for LeetCode 089 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.
解题思路:
格雷码,学过通信原理的童鞋应该都不陌生,只不过格雷码的生成方法不是按照题目中描述的那样,下面科普下格雷码的生成方法:
n=1 得到0 1
n=2 得到00 01 11 10(注意,11 和 10除了符号位外,和 00 01对称的)
n=3 得到000 001 011 010 110 111 101 100(后面四位除了符号位之外,和前四位对称)
n=4 依次类推
知道了格雷码的生成过程,那么JAVA实现如下:
static public List<Integer> grayCode(int n) {
List<Integer> list=new ArrayList<Integer>();
for(int i=0;i<Math.pow(2, n);i++){
int result=0,num=i,temp=n;
while(temp>1){
if(num>=Math.pow(2, temp-1)){
num=(int) (2*Math.pow(2, temp-1)-num-1);
result+=Math.pow(2, temp-1);
}
temp--;
}
result+=num;
list.add(result);
}
return list;
}
同时,本题有一个非常简洁的写法,如下:
public List<Integer> grayCode(int n) {
List<Integer> res = new ArrayList<Integer>();
for (int i = 0; i < (1<<n); ++i)
res.add(i ^ (i>>1));
return res;
}
Java for LeetCode 089 Gray Code的更多相关文章
- [LeetCode]题解(python):089 Gray Code
题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...
- [LeetCode] 89. Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【LeetCode】Gray Code
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
- 【leetcode】Gray Code (middle)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【题解】【排列组合】【回溯】【Leetcode】Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- leetcode[88] Gray Code
题目:格雷码. 格雷码是从0开始且之后两个相邻码之间只有一个符号不相同,例如000,100,101,111三个相邻之间只有一个二进制不同. 现在给定一个数字n,然后给出格雷码所对应的数字.例如: Fo ...
- leetCode 89.Gray Code (格雷码) 解题思路和方法
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 089 Gray Code 格雷编码
格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异.给定一个代表编码总位数的非负整数 n,打印格雷码序列.格雷码序列必须以0开头.例如, 给定 n = 2, 返回 [0,1,3 ...
- LeetCode题目:Gray Code
原题地址:https://leetcode.com/problems/gray-code/ class Solution { public: vector<int> grayCode(in ...
随机推荐
- Git之Github使用(一):Push代码到Github
Git之Github使用(一):Push代码到Github 热度 2已有 58 次阅读2016-8-26 17:56 |个人分类:常见问题|系统分类:移动开发| 互联网, commit, status ...
- MySQL GUI Tools 使用简介
转自:http://database.ctocio.com.cn/422/8919922.shtml MySQL GUI Tools是一套图形化桌面应用工具套装,可以用来管理MySQL服务器.该 ...
- MyEclipse出错解决
错误信息: Deployment failure on Tomcat 6.x. Could not copy all resources to C:\Tomcat 6.0\webapps\JavaP ...
- IDEA使用Maven打包时如何去掉测试阶段
如图
- 转: java服务器端成长指南
from: http://m.blog.csdn.net/article/details?id=45797155 前言 这是一份针对新手的服务端开发入门与进阶指南.遇到问题及时问你的 mentor ...
- 两段用来启动/重启Linux下Tomcat的Perl脚本
两段代码,第二段比较好些. 下面是Split输出结果方式的代码: #!/usr/local/bin/perl #Date:2015-07-07 print "Begin to restart ...
- GraphMatrix::BFS广度优先搜索
查找某一结点的邻居: virtual int firstNbr(int i) { return nextNbr(i, n); } //首个邻接顶点 virtual int nextNbr(int i, ...
- greenDAO学习分享总结
greenDAO(最新版本号V2.0.0的Readme) ======== greenDAO is a light & fast ORM solution for Android that m ...
- Spring使用Cache、整合Ehcache(转)
Spring使用Cache 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我 ...
- Git小玩
早就听说了GitHub的强大. 一直没有机会去看, 在公司实习的几个月里也没机会接触SVN和Git, 可是抱着对Linus大神的崇敬, 和开源的崇敬之情. 趁着不忙的几天, 来学习一下Git. 希 ...