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 ...
随机推荐
- Linux 双网卡 不同网段 网络互通
环境如下: 现状:一台linux主机上有两个网卡eth0 和eth1 ,机器能访问192网的服务资源,但不能访问10网段的资源. 要求:linux能通过eth1访问10网段的资源 路由: 网卡: 操作 ...
- VS2010 MFC中 单独添加ODBC数据库记录集类(CRecordset)方法
基于VS2010 MFC的项目是之前建好的,后来需要添加数据库. 方法分享于此. 1. 打开自己的项目,项目->添加类. 2. 选MFC ODBC使用者,点右下角的添加. 3. 点数据源. / ...
- 转:java工程师成神之路
转自: http://www.hollischuang.com/archives/489 一.基础篇 1.1 JVM 1.1.1. Java内存模型,Java内存管理,Java堆和栈,垃圾回收 htt ...
- Android 网络编程 记录
简单介绍 看了深入理解Android网络编程感觉不错.今天对Android网络编程进行了要点记录. 内容 Android基于网络技术和编程实践 要点 定义 描写叙述 IP协议 用于报文交换网络的一种面 ...
- 安卓获取软硬件信息并上传给server(Socket实现)
首先,项目结构如图--A:分为client部分CheckInfo和server端CheckInfo_Server.CheckInfo获取手机信息(Mac,Cpu,内存,已安装软件信息等)并上传到ser ...
- Mongodb性能调优
摘要 1. Mongodb 适用场景简介 2. Mongodb 性能监控与分析 3. Mongodb 性能优化建议 关于Mongodb的几个大事件 1.根据美国数据库知识大全官网发布的DB热度排行,M ...
- AutoCAD如何设置A0A1图纸
可以从网上下载相应的图纸模板,下载之后可以发现有相应的文字和模板文件 随后我们新建并找到这个dwt文件模板(比如要做一个A1的模板) 随后即可发现模板的样式,包括每种颜色的粗细,颜色和明细栏等 ...
- JavaScript中推断一个对象是否为"空对象”
JavaScript中推断一个对象是否为"空对象" 这里指的"空对象"是类似于:{ } 和 new Object() 这种. 详细的代码实现和原理例如以下: / ...
- 【Python】向函数传递列表
向函数传递列表 在实际使用中你会发现,向函数传递列表是比较实用的,这种列表可能包含名字.数字.可能更复杂的对象(字典) 假设向一个函数传递一堆水果,我们说出我们喜欢所有的水果 def Obj(frui ...
- IOS Audio开发集合
打算每天抽出一点时间学习音频方面的知识,在此做下汇总: 1. 多媒体层预览 根据结构,明确学习内容.