[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

<<左乘右除,不允许直接写2n

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

就是背:i < 2n时,i ^ i/2 异或是作差,不是01

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

class Solution {
public List<Integer> grayCode(int n) {
List<Integer> result = new LinkedList<>();
for (int i = 0; i < 1<<n; i++) result.add(i ^ i>>1);
return result;
}
}

[潜台词] :

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 (Java)

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

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

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

  4. 【LeetCode】89. Gray Code

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

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

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

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

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

  7. LeetCode OJ 89. Gray Code

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

  8. 89. Gray Code (Bit)

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

  9. 【LeetCode】89. Gray Code (2 solutions)

    Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...

随机推荐

  1. python unittest setUp 和 setUpClass 区别

    import unittest class Test(unittest.TestCase): def setUp(self): print("start!=======") def ...

  2. python实现Content-Type类型为application/x-www-form-urlencoded发送POST请求

    周日晚上接到公司的电话需要通过新榜的接口拿下微博热搜数据,拿到接口文档看了下很简单的一个post请求,主要根据时间段来获取热搜数据. 在实际编码的过程中经常遇到header的Content-Type的 ...

  3. 数组中只出现一次的数字(java实现)

    问题描述 一个整型数组里除了两个数字之外,其他的数字都出现了偶数次.请写程序找出这两个只出现一次的数字. 解题思路 如果数组中只有一个数字出现奇数次,则将数组中所有的数字做异或可得该数字. 数组中有两 ...

  4. 【C++】atof()

    转自:https://blog.csdn.net/zhaoyl03/article/details/8176387 atof 是ascII to float的缩写,它将ascII字符串转换为相应的单精 ...

  5. java代码------charAt()的用法

    总结:你看这个方法的用处真的蛮多比如这个计算器小项目,用这个charAt()方法来装运算符 package com.mmm; import java.util.Scanner; public clas ...

  6. python之路——13

    王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 复习 1.函数 1.函数的定义和调用 2.参数及传递顺序 形参角度: 位置参数:*args:默认参数:**k ...

  7. note 1 对象和数据类型

    /#行注释 print "Hello World" 对象 五种基本类型 字符串 (string),简记为str 使用 ' ' 或 " " 括起来的一系列字符 整 ...

  8. 用Jedis调用Lua脚本来完成redis的数据操作

    1.先完成一个简单的set/get操作 package com.example.HnadleTaskQueue; import redis.clients.jedis.Jedis; import ja ...

  9. win7 80端口被iis占用

    下载iis管理器,打开,关闭80端口占用

  10. JavaScript中B继承A的方法

    js继承有5种实现方式:1.继承第一种方式:对象冒充  function Parent(username){    this.username = username;    this.hello = ...