[抄题]:

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. 洋葱第4场C和D题解……

    只是记录一下: 我们机房讨论的结果……(当然有一个au爷一开始就秒掉了……首先先n^2或随机枚举出一对点根据抽屉原理可得枚举(n/2)^2+1次后至少有一对点全是对的然后对于两个对的点,首先考虑缩放比 ...

  2. edgedb 强大的对象关系数据库

    edgedb 是一个强大的对象关系数据库,构建在pg 之上. 包含的特性: 严格的强类型模式; 强大而富有表现力的查询语言; 丰富的标准库; 内置支持模式迁移; 本机GraphQL支持. 数据模型 从 ...

  3. R随机森林交叉验证 + 进度条

    library(data.table) library(randomForest) data <- iris str(data) #交叉验证,使用rf预测sepal.length k = 5 d ...

  4. js跳转 -- 转

    第一种: 复制代码代码如下: <script language="javascript" type="text/javascript"> windo ...

  5. vs2010安装的一些问题

    VS安装出现的问题一般如果出现了  基本就不会安装成功.问题出现的原因有:w7系统的版本,有些可能会安装失败,其次就是你卸载的时候不要把相应 的库及.net的库卸载  后面再安装就容易出错.这个是安装 ...

  6. centos7如何查看网络状态?

    参考https://www.jb51.net/os/RedHat/520187.html 查看网络状态: lsof -Pnl +M -i4 显示ipv4服务及监听端情况 netstat -anp 所有 ...

  7. Sqluldr2 libclntsh.so报错处理

    Sqluldr2 libclntsh.so报错处理 处理报错 [oracle@oracledg tmp]$ ./sqluldr2linux64.bin ./sqluldr2linux64.bin: e ...

  8. 6.3.4 新的_Bool类型

    如果把其他非零数值赋给_Bool类型的变量,该变量会被设置为1.这反映了C把所有的非零值都视为真. input_is_good = (scanf("%ld", &num) ...

  9. 三元运算&匿名函数lambda

    lambda # 语法: # 参数 : 返回值 # 1.不带参数的lambda表达式 def func(): return '开挂的人生不需要解释' func = lambda : '开挂的人上不需要 ...

  10. 高性能mysql 第五章 索引部分总结

    高性能索引 1.索引基础:索引的作用类似'目录'帮助Query来快速定位数据行. 1.1索引类型: 1.1.1 b-tree索引 b-tree(balance tree)索引:使用平衡树(非平衡二叉树 ...