题目来源


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. 手机开发必备技巧:javascript及CSS功能代码分享

    1. viewport: 也就是可视区域.对于桌面浏览器,我们都很清楚viewport是什么,就是出去了所有工具栏.状态栏.滚动条等等之后用于看网页的区域,这是真正有效的区域.由于移动设备屏幕宽度不同 ...

  2. Bestcoder Round# 80

    [1003 Sequence] 指数循环节,注意a mod p = 0的情况.此时你的循环节如果返回0,这时你会输出1,而实际上应该是0 #include <algorithm> #inc ...

  3. javascript第三弹——数组

    什么是数组 数组是值的有序集合.每个值叫做元素,每个元素在数组中都有数字位置编号,也就是索引.JS中的数组是弱类型的,数组中可以含有不同类型的元素.数组元素甚至可以是对象或其它数组.数组的长度是动态的 ...

  4. jquery面试题里 缓存问题如何解决?

    jquery面试题里 缓存问题如何解决? 如果直接用jQuery里的$.ajax()方法的话,去除缓存很简单,只需要配置一下缓存属性cache为false,但如果想要简单写法getJSON(),去除缓 ...

  5. PS如何查找自己想要的字体

    曾经很多人问我,这是什么字体,答曰:“不知道”,自己平时在作图时也经常会遇到这样的问题,于是我就把文字打出来,然后比照PS里边已经导入的字体挨个试验,这样不仅浪费了大量的时间和精力,而且比对完后,各种 ...

  6. hdu A strange lift

    有起点和终点,有方向,有最少次数,所以这道题很明显是一道bfs的题目,这题要利用vist数组来标记已走过的楼层,因为这题里面已走过的楼层是不可能在走第二遍的. 第二次走和第一次走的选择没有任何的区别. ...

  7. 常用的一些webshell木马官方后门

    80SEC剑心修改过世界杀毒版 http://wwwxx.cn/1.asp?web2a2dmin=//?web2a2dmin=//1.asp  绕过 不灭之魂的后门 1.在错误页面右键可以查看密码 2 ...

  8. MHA监控进程异常退出

    这两天遇到一个非常诡异的问题,打算和大家分享一下.只所以诡异估计是自己知识面不够吧.线上的MHA一直没有开启自动切换,都是手动切换的,最近开启了自动切换以后,退出securecrt窗口以后发现监控进程 ...

  9. 从eclipse到Android studio/迁移eclipse的Android项目到Android studio平台的注意事项

    整体要注意的地方 先说明一下整体需要注意的地方 1在Android studio建立项目的时候,要注意包名和原来的完全一致,不然会有很多需要改动. 2依赖的jar一定一定要找齐,不然新建项目引用不到, ...

  10. eXosip2代码分析

    主要类型定义: 1.struct eXtl_protocol struct eXtl_protocol { int enabled; int proto_port; ]; ]; int proto_n ...