Description:

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 计算格雷码的十进制数。套用格雷码编码公式解决。
public class Solution {
public List<Integer> grayCode(int n) {
List<Integer> res = new ArrayList<Integer>(); for(int i=0; i<(1<<n); i++)
res.add(i/2 ^ i);
return res;
}
}
 

LeetCode——Gray Code的更多相关文章

  1. [LeetCode] Gray Code 格雷码

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  2. [leetcode]Gray Code @ Python

    原题地址:https://oj.leetcode.com/problems/gray-code/ 题意: The gray code is a binary numeral system where ...

  3. LeetCode:Gray Code(格雷码)

    题目链接 The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...

  4. LeetCode: Gray Code [089]

    [题目] The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...

  5. LeetCode: Gray Code 解题报告

    Gray CodeThe gray code is a binary numeral system where two successive values differ in only one bit ...

  6. [转载]LeetCode: Gray Code

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  7. Leetcode Gray Code

    题目的意思就是将十进制转换成格雷码 首先将二进制转换成格雷码 根据此图可以看出二进制的第i和第i+1位异或为格雷码的第i+1位,对于给定的十进制数x,其(x>>1)相当于二进制向右移动一位 ...

  8. [LeetCode]题解(python):089 Gray Code

    题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...

  9. 【一天一道LeetCode】#89. Gray Code

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...

随机推荐

  1. Serializable 和 Parcelable 区别

    http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0204/2410.html http://www.cnblogs.com/blu ...

  2. Determining if a point lies on the interior of a polygon

    Determining if a point lies on the interior of a polygon Written by Paul Bourke  November 1987 Solut ...

  3. mybatis 3.2.3 maven dependency pom.xml 配置

    <dependency>      <groupId>org.mybatis</groupId>      <artifactId>mybatis< ...

  4. Markdown学习语法的记录

    标题示例 === 标题示例 ---- #标题示例 ##标题示例 ###标题示例 >引用示例 >>引用示例 >>> 引用示例 _斜体示例_ *斜体示例* __粗体示例 ...

  5. 解决genemotion模拟器冲突导致的Android Studio无法启动ADB的问题

    首先命令行下运行 adb nodaemon server ./adb nodaemon server (Mac OSX) 如果出现错误: error: could not install *smart ...

  6. Ubuntu server 搭建Git server

    Ubuntu server 搭建Git server,git相比svn,最主要就是分布式了,每个客户端用户的本地都是一个版本管理控制器. Ubuntu server 版本为12.04 搭建步骤如下: ...

  7. Xshell中文乱码问题

    1.  先查看当前使用的语言: echo $LANG 2.  查看系统的语言安装包: locale 3.  如果没有中文安装包(包含zh_CN字样),需要网络或者自己上传安装包,安装 4.  有了中文 ...

  8. html5media.js 让浏览器兼容<Video><Audio> 标签

    介绍:https://html5media.info/ 项目:https://github.com/etianen/html5media Wiki:https://github.com/etianen ...

  9. LCLFramework框架之Plugin模式

    插件应用架构概述 基于LCLFramework插件框架的应用由以下三个部分构成: (1)主程序:针对特定应用环境(Web.WinForm等应用环境),加载启动插件,获取插件入口,运行入口程序. (2) ...

  10. C#打开摄像头抓取照片然后退出

    using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; namespa ...