[LeetCode]题解(python):089 Gray Code
题目来源
https://leetcode.com/problems/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.
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.
题意分析
Input:
:type n: int
Output:
:rtype: List[int]
Conditions:给定一个大小n(即n bit),返回bit为n位下的Gray code序列。
题目思路
Binary Code可以直接转为Gray Code,比如Binary Code 为 1011,转为为Gray Code:
(Binary Code)1011 = 1(第一位不变), 1(第一位异或第二位,1^0), 1(第二位异或第三位,0^1), 0(第三位异或第四位,1^1) = 1110(Gray Code)
也就是(1011 >> 1) ^ 1011 = 1110, 一般化即: (i >> 1) ^ i
AC代码(Python)
class Solution(object):
def grayCode(self, n):
"""
:type n: int
:rtype: List[int]
"""
if n <= 0:
return [0]
size = 1 << n
res = []
for i in range(size):
res.append((i>>1)^i)
return res
[LeetCode]题解(python):089 Gray Code的更多相关文章
- Java for LeetCode 089 Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [leetcode.com]算法题目 - Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- LeetCode(87) Gray Code
题目 The gray code is a binary numeral system where two successive values differ in only one bit. Give ...
- 089 Gray Code 格雷编码
格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异.给定一个代表编码总位数的非负整数 n,打印格雷码序列.格雷码序列必须以0开头.例如, 给定 n = 2, 返回 [0,1,3 ...
- LeetCode题解之Unique Morse Code Words
1.题目描述 2.题目分析 将words 中的每一个string 直接翻译成对应的Morse 码,然后将其放入 set 中,最后返回set的大小即可,此处利用的set 中元素不重复的性质. 3.代码 ...
- [leetcode]Gray Code @ Python
原题地址:https://oj.leetcode.com/problems/gray-code/ 题意: The gray code is a binary numeral system where ...
- 【LeetCode】89. Gray Code 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【题解】【排列组合】【回溯】【Leetcode】Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- LeetCode: Gray Code [089]
[题目] The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...
随机推荐
- POJ 2486 Apple Tree(树形DP)
题目链接 树形DP很弱啊,开始看题,觉得貌似挺简单的,然后发现貌似还可以往回走...然后就不知道怎么做了... 看看了题解http://www.cnblogs.com/wuyiqi/archive/2 ...
- Shell 操作练习
#! /bin/sh ############################### # -- # # author jackluo # # net.webjoy@gmail.com # ###### ...
- iOS真机调试的验证项
基本的匹配项 App 描述文件 BundleID (匹配) ApplicationID Cert (匹配) Cert DeviceID (被包含) DeviceIDs 描述文件就是包含了各种需要验证信 ...
- hdu Watch The Movie
这道题是二维背包的问题,因为这道题里面有时间l和可选数量m两个约束条件.只要0/1背包的基础上再加上一重循环即可,这题需要注意的是初始化的问题,初始化时只有m=0时dp数组为0,其它置为负数.再一个就 ...
- ThinkPHP 中M方法和D方法详解----转载
转载的地址,http://blog.163.com/litianyichuanqi@126/blog/static/115979441201223043452383/ 自己学到这里的时候,不能清除的分 ...
- 使用SimpleXML应该注意的问题有哪些?
SimpleXML提供了一套简单快速的XML操作方法,大大地提高了XML操作的效率. 但是有时不小心也会带来不小的麻烦,看下面一段代码: $xml=simplexml_load_string('< ...
- 根据PHP手册什么叫作变量的变量?
在最近做的一个项目中,发现了一个新的概念,关于在PHP中使用变量的变量.在的程序中,需要在一个页面同时更新多个记录,在经过相当长时间的痛苦思索之后,脑海中偶然地闪现出了变量的变量(variable v ...
- 配置Log4j(很详细)
Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息.通过配置,可以创建出Log4J的运行环境 ...
- 如何使用 Migration创建一个迁移
切换到YII所在的目录 yii migrate/create test
- [学习笔记]ESXI学习记录
vmware workstation环境下,可以为虚拟机添加多块网卡.在虚拟机上单击右键,settings 界面,选中network adapter,然后通过custom选项,可以将多个不同的虚拟机使 ...