89. Gray Code返回位运算的所有生成值
[抄题]:
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返回位运算的所有生成值的更多相关文章
- 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 (Java)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【LeetCode】89. Gray Code 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【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 ...
- 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 ...
- 【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 ...
随机推荐
- COMBIN14简单应用
目录 案例1 说明 APDL代码 结果 案例2 说明 APDL代码 结果 案例3 说明 APDL代码 结果 参考网址:http://blog.sina.com.cn/s/blog_65936c2301 ...
- Vue列表组件与弹窗组件示例
列表组件 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <me ...
- PythonStudy——Python 内置函数 Built-in function
内置方法:Python中声明每一个类系统都会加上一些默认内置方法,提供给系统调用该类的对象时使用.比如需要实例化一个对象时,需要调用该类的init方法:使用print去打印一个类时,其实调用的是str ...
- php输出数据到csv文件
function export() { $fileName = date('Y-m-d').uniqid().'.csv'; set_time_limit(0); ini_set('memory_li ...
- Linux 文件查看,文件夹切换,权限查看
当前用户只操作当前用户目录 1. 输入终端显示内容: 用户 @ 系统 : 路径信息 $ $ 表示普通用户 家目录 # 表示超级用户 家目录 [sudo -i ] 使用root用户 : 使用 ...
- putty安装和使用
https://blog.csdn.net/l707941510/article/details/80520790
- Linux网站运维工程师基础大纲
第一阶段:Linux运维基础 第一章:Linux基础以及入门介绍 1.Linux硬件基础 2.Linux发展过程 3.创建虚拟机和系统安装 第二章:Linux系统目录结构介绍 1.Linux系统优化 ...
- mysql5.7.18.1修改用户密码报错ERROR 1054 (42S22): Unknown column 'password' in 'field list'解决办法
本意向修改一个用户的密码,网上搜到的命令为如下 mysql> update user set password=password(“新密码”) where user=”用户名”; 执行后报错 E ...
- django之Models和ORM
ORM Object Relational Mapping,简称ORM,是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 通过使用描述对象和数据库之间映射的元数据,将程序中的对象自动持久 ...
- jqGrid基本用法与示例
转自:https://chuanlu.iteye.com/blog/1953544 一.jqGrid的基本用法 1.html页面 <!DOCTYPE html PUBLIC "-//W ...