力扣—gray code (格雷编码) python实现
题目描述:
中文:
格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异。
给定一个代表编码总位数的非负整数 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实现的更多相关文章
- 089 Gray Code 格雷编码
格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异.给定一个代表编码总位数的非负整数 n,打印格雷码序列.格雷码序列必须以0开头.例如, 给定 n = 2, 返回 [0,1,3 ...
- Leetcode89. Gray Code格雷编码
给定一个代表编码总位数的非负整数 n,打印其格雷编码序列.格雷编码序列必须以 0 开头. 示例 1: 输入: 2 输出: [0,1,3,2] 解释: 00 - 0 01 - 1 11 - 3 10 - ...
- [LeetCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [LeetCode] 89. Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [LintCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- gray code 格雷码 递归
格雷码 the n-1 bit code, with 0 prepended to each word, followd by the n-1 bit code in reverse order, w ...
- Gray Code - 格雷码
基本概念 格雷码是一种准权码,具有一种反射特性和循环特性的单步自补码,它的循环.单步特性消除了随机取数时出现重大误差的可能,它的反射.自补特性使得求反非常方便.格雷码属于可靠性编码,是一种错误最小化的 ...
- HDU 5375 Gray code 格雷码(水题)
题意:给一个二进制数(包含3种符号:'0' '1' '?' ,问号可随意 ),要求将其转成格雷码,给一个序列a,若转成的格雷码第i位为1,则得分+a[i].求填充问号使得得分最多. 思路:如果了 ...
- [Swift]LeetCode89. 格雷编码 | Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
随机推荐
- python写txt文件
with open('data.txt','w') as f: #设置文件对象 w是重新写,原来的会被抹掉,a+是在原来的基础上写 str0=u"写文件\n" #写中文要在字符串签 ...
- INSTR代替NOT LIKE
instr(title,'手册')>0 相当于 title like '%手册%' instr(title,'手册')=1 相当于 title like '手册%' instr(titl ...
- springMVC使用map接收入参 + mybatis使用map 传入查询参数
测试例子: controllel层 ,使用map接收请求参数,通过Debug可以看到,请求中的参数的值都是字符串形式,如果将这个接收参数的map直接传入service,mybatis接收参数时会报错, ...
- springmvc的请求参数
@RequestMapping("/testRequestParam") public String testRequestParam(@RequestParam("us ...
- phpstudy的80端口被占用问题
1.查看电脑中当前程序占用的端口和程序ID 当phpstudy设置端口号为80时显示占用,在命令行中输入:netstat -ano 就可以看到当前电脑中程序占用的端口和程序ID等等信息 2.查看正在运 ...
- PHP基于PDO实现的SQLite操作类
<?php // sqlite分页类 class SqliteDB{ public function __construct(){ // 初始化数据库,并且连接数据库 数据库配置 $this-& ...
- BUUCTF | 摩丝
将得到的交上去居然不对: 然而大写却过了: flag{ILOVEYOU} 因为摩斯电码在设计的时候就没有区分大小写,而且从码表中可以看到,都是大写,所以在网站上解密出来的自己转成大写
- python中模块介绍
一,模块概念 在计算机程序开发的过程当中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护.为了编码更加容易维护,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码 ...
- liunx 上无法kill 掉 redis服务
要新学习一下redis 的哨兵服务,但是发现启动redis的时候,哨兵服务已经存在了,而且reids6379的服务也杀不死,就找到这样的参考方案 /etc/init.d/redis-server st ...
- JS:收集的一些Array及String原型对象的扩展实现代码
扩展Array的原型对象的方法 // 删除数组中数据 Array.prototype.del = function(n) { if (n<0) return this; return this ...