题目来源


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的更多相关文章

  1. 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 ...

  2. [leetcode.com]算法题目 - Gray Code

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

  3. LeetCode(87) Gray Code

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

  4. 089 Gray Code 格雷编码

    格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异.给定一个代表编码总位数的非负整数 n,打印格雷码序列.格雷码序列必须以0开头.例如, 给定 n = 2, 返回 [0,1,3 ...

  5. LeetCode题解之Unique Morse Code Words

    1.题目描述 2.题目分析 将words 中的每一个string  直接翻译成对应的Morse 码,然后将其放入 set 中,最后返回set的大小即可,此处利用的set 中元素不重复的性质. 3.代码 ...

  6. [leetcode]Gray Code @ Python

    原题地址:https://oj.leetcode.com/problems/gray-code/ 题意: The gray code is a binary numeral system where ...

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

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

  8. 【题解】【排列组合】【回溯】【Leetcode】Gray Code

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

  9. LeetCode: Gray Code [089]

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

随机推荐

  1. python 代码片段9

    #coding=utf-8 # 字符串指示符号 r表示raw u表示unicode mystring=u'this is unicode!--by' print mystring # 'raw'表示告 ...

  2. BZOJ2862 : 分糖果

    二分答案$x$表示最大的一段的和. 设$f[i]$表示前$i$个最多分几段,满足最大的一段不超过$x$,若$f[n]\geq k$,则可行, 则$f[i]=\max(f[j])+1,sum[i]-su ...

  3. 使用freemarker生成word,步骤详解并奉上源代码

    1.   步骤 1.    用word编辑好模板 1. 普通字符串替换为 ${string} 2. 表格循环用标签 <#list userList as user> 姓名:${user.u ...

  4. jsoup httpclient 爬取网页并下载google图标

    jsoup下载地址 http://www.jsoup.org httpclient下载地址 http://hc.apache.org/downloads.cgi 其他jar包见附件 Crawler p ...

  5. android之发送短信程序

    首先改写activity_main.xml文件 代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/re ...

  6. Hbuild - 使用海马玩模拟器调试

    http://ask.dcloud.net.cn/question/13605     通过海马玩CMD(Droid4X_Settings_Tools_v1.1.5.bat) 设置模拟器的端口 269 ...

  7. HDU 1698 Just a Hook(线段树区间替换)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. DS实验题 Missile

    题目: 提示:并没有精度问题. 原题 NOIP2010 导弹拦截 思路 设源点为A(x1, y1)和B(x2, y2). 第一步,用结构体存节点,包括以下元素: 1.横坐标x 2.纵坐标y 3.节点和 ...

  9. dp和px的转换

    /** * dp转px * @param context * @param dp * @return */ public static int dp2px(Context context, float ...

  10. 20145235 《Java程序设计》第4周学习总结

    代码托管截图 教材学习内容总结 继承 •继承:继承基本上就是避免多个类间重复定义共同行为. 我理解的就是:在编写程序的过程中可能会出现部分代码重复的现象,把重复的部分单独定义为一类(父类),在其他代码 ...