题目描述:

中文:

格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异。

给定一个代表编码总位数的非负整数 n,打印其格雷编码序列。格雷编码序列必须以 0 开头。

英文:

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.

class Solution(object):
def grayCode(self, n):
"""
:type n: int
:rtype: List[int]
"""
res = [0]
for i in range(n):
res += [ (1 << i) + res[-j-1] for j in range(1 << i) ]
return res

题目来源:力扣

力扣—gray code (格雷编码) python实现的更多相关文章

  1. 089 Gray Code 格雷编码

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

  2. Leetcode89. Gray Code格雷编码

    给定一个代表编码总位数的非负整数 n,打印其格雷编码序列.格雷编码序列必须以 0 开头. 示例 1: 输入: 2 输出: [0,1,3,2] 解释: 00 - 0 01 - 1 11 - 3 10 - ...

  3. [LeetCode] Gray Code 格雷码

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

  4. [LeetCode] 89. Gray Code 格雷码

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

  5. [LintCode] Gray Code 格雷码

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

  6. gray code 格雷码 递归

    格雷码 the n-1 bit code, with 0 prepended to each word, followd by the n-1 bit code in reverse order, w ...

  7. Gray Code - 格雷码

    基本概念 格雷码是一种准权码,具有一种反射特性和循环特性的单步自补码,它的循环.单步特性消除了随机取数时出现重大误差的可能,它的反射.自补特性使得求反非常方便.格雷码属于可靠性编码,是一种错误最小化的 ...

  8. HDU 5375 Gray code 格雷码(水题)

    题意:给一个二进制数(包含3种符号:'0'  '1'  '?'  ,问号可随意 ),要求将其转成格雷码,给一个序列a,若转成的格雷码第i位为1,则得分+a[i].求填充问号使得得分最多. 思路:如果了 ...

  9. [Swift]LeetCode89. 格雷编码 | Gray Code

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

随机推荐

  1. 【串线篇】SQL映射文件select简单查询标签

    一.参数(Parameters)传递 单个参数 基本类型:取值#{hahaha}随便写 多个参数 <!--   public Employee getEmpById(Integer id,Str ...

  2. RabbitMQ之交换机

    1. 交换机类型 rabbitmq常见有四种交换机类型: direct, topic, fanout, headers. 一般headers都不用,工作中用得较多的是fanout,它会将消息推送到所有 ...

  3. Angular JS - 8 - SeaJS与前端模块化

    一.前端模块化 关于前端模块化,参考以下链接 : https://github.com/seajs/seajs/issues/547 http://www.cnblogs.com/huiguo/cat ...

  4. Activation functions on the Keras

    sigmoid tanh tanh函数定义如下: 激活函数形状: ReLU 大家族 ReLU softmax 函数 softmax是一个函数,其主要用于输出节点的分类,它有一个特点,所以的值相加会等于 ...

  5. Yii2 kineditor

    用 kineditor实现异步 <table cellspadding=5 width=400> <tr height='150'> <td valign="t ...

  6. 【CF1257A】Two Rival Students【思维】

    题意:给你n个人和两个尖子生a,b,你可以操作k次,每次操作交换相邻两个人的位置,求问操作k次以内使得ab两人距离最远是多少 题解:贪心尽可能的将两人往两边移动,总结一下就是min(n-1,|a-b| ...

  7. 初识 ❤ TensorFlow |【一见倾心】

    说明

  8. Gitblit用户没有push权限,但是已经在team里面配置了

    问题: 用户已经移动到team里面,team有对应repository的push权限. does not have push permissions for 解决方案: 发现这个用户以前单独配置了这个 ...

  9. STL排序函数

    Qsort,Sort,Stable_sort,Partial_sort,List::sort 参考

  10. oauth2学习

    oauth2 生词: 授权码模式(authorization code) 简化模式(implicit) 密码模式(resource owner password credentials) 客户端模式( ...