89. Gray Code (Java)
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.
Example 1:
Input: 2
Output:[0,1,3,2]
Explanation:
00 - 0
01 - 1
11 - 3
10 - 2 For a given n, a gray code sequence may not be uniquely defined.
For example, [0,2,3,1] is also a valid gray code sequence. 00 - 0
10 - 2
11 - 3
01 - 1
Example 2:
Input: 0
Output:[0]
Explanation: We define the gray code sequence to begin with 0.
A gray code sequence of n has size = 2n, which for n = 0 the size is 20 = 1.
Therefore, for n = 0 the gray code sequence is [0].
规律:
1. 如果格雷码n位数,那么返回的数组长度是2^n(即1 << n)
2. 二进制-->格雷码:如果二进制码字的第i位和i+1位相同,则对应的格雷码的第i位为0,否则为1。最高为之前可以看作还有一位0,即最高位是和0异或。
*格雷码-->二进制:最左边一位不变,从左边第二位起,将每位与左边一位解码后的值异或,作为该位解码后的值。
class Solution {
public List<Integer> grayCode(int n) {
int length = 1 << n;
List<Integer> ret = new ArrayList<Integer>();
ret.add(0);
int i = 1;
int num;
while(i < length){
num = i^(i>>1); //异或,相同为0,不同为1
ret.add(num);
i++;
}
return ret;
}
}
89. Gray Code (Java)的更多相关文章
- 89. Gray Code - LeetCode
Question 89. Gray Code Solution 思路: n = 0 0 n = 1 0 1 n = 2 00 01 10 11 n = 3 000 001 010 011 100 10 ...
- 89. Gray Code
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
- [LeetCode] 89. Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【LeetCode】89. Gray Code
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
- 89. Gray Code(中等,了解啥是 gray code)
知道啥是 gray code 就是收获了. 下面介绍了 gray code 发明的 motivation, 了解动机后就知道啥是 gray code 了. https://zh.wikipedia.o ...
- 【一天一道LeetCode】#89. Gray Code
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...
- 89. Gray Code返回位运算的所有生成值
[抄题]: The gray code is a binary numeral system where two successive values differ in only one bit. G ...
- LeetCode OJ 89. Gray Code
题目 The gray code is a binary numeral system where two successive values differ in only one bit. Give ...
- 89. Gray Code (Bit)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
随机推荐
- 如何利用css进行网页布局
一.单列布局(类似于搜狐网站) 如: 代码为: 二.两列布局 1.固定宽度 代码为: 2.自适应 代码为: 三.三列布局 代码为: 四.混合布局 就是在前面的基础上,在进行划分块 如: 代码为:
- Mac 配置adb环境变量(为了开Appium)亲测
1.启动终端Terminal. 2.进入当前用户的home目录. 在命令行输入echo $HOME 3.创建.bash_profile文件. 命令行输入touch .bash_profile 4.打开 ...
- LC 431. Encode N-ary Tree to Binary Tree 【lock,hard】
Design an algorithm to encode an N-ary tree into a binary tree and decode the binary tree to get the ...
- background-color 属性
background-color:transparent;是什么意思?? 把背景颜色设为透明色
- linux添加用户所在群组
etc目录下面有两个文件一个passwd一个grouppasswd里gid是主组,其他组是扩展组,扩展组在/etc/group里描述.useradd username如果不指定,默认创建一个与uid相 ...
- ubuntu14.04 安装apache+php7.2*
1.安装apache2 sudo apt-get update sudo apt-get install apache2 这时http://你机器的ip,就可以访问了 2.安装php7.0+及一些必要 ...
- 工程变更(ENGINEERING CHANGE)
工程变更(ENGINEERING CHANGE)是企业活动重要的管制项目之一,依照实施的时间.目的不同,其管制细分如下: ECN (ENGINEERING CHANGE NOTICE)工程变更通知: ...
- 抓包工具chlers的使用
1,下载chlers破解版:https://zhubangbang.com/charles-crack-version-free-download-and-install-tutorial.html ...
- c++11实现线程池
http://note.youdao.com/noteshare?id=de17ff681b277bb914ef46aeb9271fc4
- HCL试验4
PC端配置:配置ip地址 配置网关 交换机配置:①创建VLAN system-view vlan 10 vlan 20 ②配置PC端接口 interface vlan-interface 10 ip ...