原题地址: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. 在vi 按了Ctrl s 之后..

    习惯了在windows下写程序,也习惯了按ctrl+s 保存代码,在用vi的时候,也习惯性的按了ctrl+s 然后vi终端就像卡住了一样. 原来: ctrl+s 终止屏幕输出(即停止回显),你敲的依然 ...

  2. 【bzoj 1076】【SCOI2008】奖励关

    1076: [SCOI2008]奖励关 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1602  Solved: 891[Submit][Status ...

  3. Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集

    D. Mahmoud and a Dictionary 题目连接: http://codeforces.com/contest/766/problem/D Description Mahmoud wa ...

  4. BZOJ 3245: 最快路线 spfa

    3245: 最快路线 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3245 Description 精明的小R每每开车出行总是喜欢走最快 ...

  5. 一些收集的MikroTik RouterOS破解版虚拟机VMware

    会不定期更新,也许后续自己来做,现在是收集.持续关注这个分享链接即可. 链接:https://pan.baidu.com/s/1j7ciesPm1yAgCJ26dLJ8sg  密码:i64w

  6. 用.Net如何访问Linux下目录

    很多Windows下的应用需要访问和监控Linux下的目录,本文便介绍如何实现. 只需要搭建配置samba服务,即可将Linux下的目录变得如同Windows下共享可写. 1.服务查询 默认情况下,L ...

  7. C# webrequest 抓取数据时,多个域Cookie的问题

    最近研究了下如何抓取为知笔记的内容,在抓取笔记里的图片内容时,老是提示403错误,用Chorme的开发者工具看了下: 这里的Cookie来自两个域,估计为知那边是验证了token(登录后才能获取到to ...

  8. mysql函数的使用

    最近总感觉sql语句不对劲,所以就看了一些官方文档发现了一些以前没有注意的函数:感觉在查询的时候可以用得上,毕竟是内置函数,用起来效率应该会好一些的: FIND_IN_SET(str,strlist) ...

  9. Revit API遍历全部风管,找到与风管相关的墙开洞

    涉及向量计算,求相交等相关技术. )                 {                     foreach (Face face in solid.Faces)          ...

  10. 用Qemu搭建x86学习环境

    作者信息 作者:彭东林 邮箱:pengdonglin137@163.com QQ:405728433 软件平台 主机: Ubuntu14.04 64位版本 模拟器:Qemu-2.8.0 Linux内核 ...