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 ...
随机推荐
- Jsp2.0自定义标签(第二天)——自定义循环标签
今天是学习自定义标签的第二天,主要是写一个自定义的循环标签. 先看效果图: 前台页面Jsp代码 <%@ page language="java" contentType=&q ...
- web.xml文件的 xsd引用(或dtd引用)学习
1. 为什么web.xml会有不同版本的xsd引用: JDK依赖变化: 或 servlet(JAVA EE)自身API的改变: 2. 为什么会有dtd和xsd两个版本的区别 我是在这篇文章中看到的,作 ...
- 在Delphi中应用AOP实现日志功能
AOP现在很火,网上有这许多支持AOP的框架,对于Delphi来说同样也有MeAOP.不过觉得这些框架太复杂了. 现在有一个系统,基本上都快结束了,整体上当然是没有采用什么AOP的框架.对于这样的系统 ...
- GlusterFS分布式文件系统高速管理
TaoCloud XDFS基于GlusterFS开源分布式文件系统,进行了系统优化.project化.定制化和产品化工作,五年以上的实践积累了大量实践经验,包含客户案例.最佳实践.定制开发.咨询服务和 ...
- binary-tree-inorder-traversal——二叉树中序遍历
Given a binary tree, return the inordertraversal of its nodes' values. For example:Given binary tree ...
- Android Design Support Library(2)- TextInputLayout的使用
原创文章,转载请注明 http://blog.csdn.net/leejizhou/article/details/50494634 这篇文章介绍下Android Design Support Lib ...
- (五)解决jQuery和其它库的冲突
在jQuery库中,几乎所有的插件都被限制在它的命名空间里.全局的对象都很好地存储在jQuery命名空间里,因此当把jQuery和其它javascript类库一起使用时,不会引起冲突.(注意:默认情况 ...
- 在 Selenium 中让 PhantomJS 执行它的 API
from selenium import webdriver driver = webdriver.PhantomJS() script = "var page = this; page.o ...
- 硬件问题大杂烩&Coffee lake框图
PCB阻抗控制 https://www.cnblogs.com/lifan3a/articles/6095372.html 1.高速差分信号串联AC耦合电容什么请况下要做镂空处理: (1)为了阻抗匹配 ...
- ReactNative Navigator
https://facebook.github.io/react-native/docs/navigator.html Navigator实现了页面之间的跳转. Demo描述:打开即进入“课程”页面, ...