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.

公式:

G(i) = i^ (i/2).

 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^i>>1);
return res;
}
}

89. Gray Code(公式题)的更多相关文章

  1. 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 ...

  2. 89. Gray Code(中等,了解啥是 gray code)

    知道啥是 gray code 就是收获了. 下面介绍了 gray code 发明的 motivation, 了解动机后就知道啥是 gray code 了. https://zh.wikipedia.o ...

  3. 89. Gray Code返回位运算的所有生成值

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

  4. leetCode 89.Gray Code (格雷码) 解题思路和方法

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

  5. 【LeetCode】89. Gray Code 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 89. Gray Code

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

  7. 【LeetCode】89. Gray Code

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

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

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

  9. LeetCode OJ 89. Gray Code

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

随机推荐

  1. Java中自己定义缓存方式

    说说自己在开发中经常用到的写法. /** * 数据缓存 * @author * */public class DataCache {    /** 对象缓存*/    public static Ma ...

  2. STM32学习之路之MDK安装篇

  3. memset和memcpy函数、atoi函数

    memset void *memset(void *s,int c,size_t n) 总的作用:将已开辟内存空间 s 的首 n 个字节的值设为值 c.如下: // 1.将已开辟内存空间s的首n个字节 ...

  4. php学习一:语法规则

    1.书写规则 在html中嵌入php的时候,需要有结束语,即<?php ...?>,在靠近结束符号的最后一个语句可以不用写分号: 但是在单独的php中,最后可以不用以?>来结尾; 2 ...

  5. Swift/Objective-C-Swift与Objective-C混用教程

    简介:我想很多iOS开发者在知道Swift后,心中最大的问题就是如何将Swift应用到原有项目之中.下面我将简要介绍这2种语言的混用方法,内容参考自官方文档 Using Swift with Coco ...

  6. 【Laravel】Mac下玩转Laravel

    1 apache 首先Mac系统是自带了Apache,只需要执行 sudo apachectl start 就可以打开Apache服务,然后访问 http://localhost 就可以访问到,it' ...

  7. Java 去除List列表中的重复项

    /** * Remove list duplicate item * * @param srcList * @return */ private static ArrayList<Resolve ...

  8. WEB安全第一篇--对服务器的致命一击:代码与命令注入

    零.前言 最近做专心web安全有一段时间了,但是目测后面的活会有些复杂,涉及到更多的中间件.底层安全.漏洞研究与安全建设等越来越复杂的东东,所以在这里想写一个系列关于web安全基础以及一些讨巧的pay ...

  9. 用C或C++为Python编写模块

    1.使用c或c++编写对应的函数例如: //modtest.c int abs(int number){ ){ return -number; } else{ return number; } } 2 ...

  10. nodejs MySQL操作

    一  wamp创建数据库 选择phpMyAdmin 选择用户,添加用户 填写数据库详细资料,填写完毕选择右下角的“执行” 用户添加成功 2. nodejs 安装mysql驱动  npm install ...