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 ...
随机推荐
- OpenLDAP一登录系统就修改密码
1:修改配置文件 在前面打开注释 moduleload ppolicy.la modulepath /usr/lib/openldap modulepath /usr/lib64/openldap ...
- lvm基本管理
LVM简介 LVM (logical volume manager)逻辑卷管理的简写,可以动态增加或减小逻辑卷的大小. 术语介绍 物理存储介质(Physical Storage Media) 通常指硬 ...
- Bootice1.34版本把grub4dos0.46a写入硬盘MBR失败一个例子
Bootice1.34版本把grub4dos0.46a写入硬盘MBR失败一个例子 一个同事的台式机,BIOS启动,500GB硬盘,分了四个MBR分区,C盘是激活的主分区,第二个分区50 ...
- 【ELK】之Centos6.9_x64安装elasticsearch6.2.1
1.下载elasticsearch6.2.1 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.1 ...
- Linux系统重置root用户密码
Linux系统重置root用户密码 作者:Eric 微信:loveoracle11g 查看系统版本是不是RHEL7 [root@zhouwanchun ~]# cat /etc/redhat-rele ...
- python常见用法
1.冒泡排序 a = [25,15,47,36,44,455,67,234,7,8,-47] def sortport(): for i in range(len(a)-1): for j in ra ...
- win10 安全设置
风险程序: C:\Users\dong\Downloads\KMSTools_V18.06.2016_Xitongzhijia\KMSTools.exe 发起来源:C:\Windows\Syste ...
- hive 表锁和解锁
场景: 在执行insert into或insert overwrite任务时,中途手动将程序停掉,会出现卡死情况(无法提交MapReduce),只能执行查询操作,而drop insert操作均不可操作 ...
- 阿里云RDS读写分离数据查询延迟解决
mysql使用RDS做数据主从读写分离.在使用的过程中发现部分业务对其他服务以来严重.但是由于系统不是采用微服务的架构,造成部分数据插入数据库后,后续操作读取数据库没有查询到前面插入的数据.查看阿里云 ...
- python批量处理文件夹中文件的问题
用os模块读取文件夹中文件 原来的代码: import osfrom scipy.misc import imread filenames=os.listdir(r'./unprocess')for ...