原题地址:https://oj.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.

For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:

00 - 0
01 - 1
11 - 3
10 - 2

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.

For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.

解题思路:格雷码的生成,采用数学的方法。

代码:

class Solution:
# @return a list of integers
def grayCode(self, n):
res=[]
size=1<<n
for i in range(size):
res.append((i>>1)^i)
return res

[leetcode]Gray Code @ Python的更多相关文章

  1. [LeetCode] Gray Code 格雷码

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

  2. LeetCode——Gray Code

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

  3. LeetCode:Gray Code(格雷码)

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

  4. LeetCode: Gray Code [089]

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

  5. LeetCode: Gray Code 解题报告

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

  6. [转载]LeetCode: Gray Code

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

  7. Leetcode Gray Code

    题目的意思就是将十进制转换成格雷码 首先将二进制转换成格雷码 根据此图可以看出二进制的第i和第i+1位异或为格雷码的第i+1位,对于给定的十进制数x,其(x>>1)相当于二进制向右移动一位 ...

  8. [LeetCode]题解(python):089 Gray Code

    题目来源 https://leetcode.com/problems/gray-code/ The gray code is a binary numeral system where two suc ...

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

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

随机推荐

  1. iOS 11开发教程(十四)iOS11应用代码添加视图

    iOS 11开发教程(十四)iOS11应用代码添加视图 如果开发者想要使用代码为主视图添加视图,该怎么办呢.以下将为开发者解决这一问题.要使用代码为主视图添加视图需要实现3个步骤. (1)实例化视图对 ...

  2. BZOJ.1061.[NOI2008]志愿者招募(线性规划 对偶原理 单纯形 / 费用流SPFA)

    题目链接 线性规划 用\(A_{ij}=0/1\)表示第\(i\)天\(j\)类志愿者能否被招募,\(x_i\)为\(i\)类志愿者招募了多少人,\(need_i\)表示第\(i\)天需要多少人,\( ...

  3. mount: unknown filesystem type 'vboxsf' centos ubuntu 处理方案

    Vagrant was unable to mount VirtualBox shared folders. This is usually because the filesystem " ...

  4. Codeforces Round #258 (Div. 2) E. Devu and Flowers 容斥

    E. Devu and Flowers 题目连接: http://codeforces.com/contest/451/problem/E Description Devu wants to deco ...

  5. ant design Modal关闭时清除数据的解决方案

    背景:modal组件关闭时不清除数据,原来输入的数据还存在 解决方案: 1.modal的api:destroyOnClose 2.手动控制modal的销毁 this.state = { destroy ...

  6. 独家专访|浙江执御:为何接受富安娜入股而不选VC?_深圳市跨境电子商务协会_新浪博客

    独家专访|浙江执御:为何接受富安娜入股而不选VC?_深圳市跨境电子商务协会_新浪博客   http://blog.sina.com.cn/s/blog_13cb5d69e0102vuvk.html

  7. 使用Gitblit 搭建Windows Git服务器

    使用Gitblit 搭建Windows Git服务器 整理使用Gitblit搭建Git服务器的步骤. 目录 使用Gitblit 搭建Windows Git服务器 目录 下载安装 配置 运行 客户端运行 ...

  8. linux 内核升级 转

    inux 内核升级 2011-03-25 23:13:28 分类: LINUX 因要测试一些软件,需要2.6.30以上的内核,安装好CentOS 5.5,内核是2.6.18-194.el5.这次的升级 ...

  9. HDU 4920 Matrix multiplication(矩阵相乘)

    各种TEL,233啊.没想到是处理掉0的情况就能够过啊.一直以为会有极端数据.没想到居然是这种啊..在网上看到了一个AC的奇妙的代码,经典的矩阵乘法,仅仅只是把最内层的枚举,移到外面就过了啊...有点 ...

  10. 使用WebRTC搭建前端视频聊天室

    在两个浏览器中,为聊天.游戏.或是文件传输等需求发送信息是十分复杂的.通常情况下,我们需要建立一台服务器来转发数据,当然规模比较大的情况下,会扩展成多个数据中心.这种情况下很容易出现很高的延迟,同时难 ...