#Leet Code# Gray Code
描述:
要求相邻数2进制差一位
先获得n-1的列表表示小于 2^(n-1) 的符合要求的列表,加上最高位的加成 2^(n-1) 就是大于等于 2^(n-1) 的符合要求的列表,后者翻转一下就能够与前者连接上了
代码:
class Solution:
# @return a list of integers
def grayCode(self, n):
if n == 0: return [0] s1 = self.grayCode(n - 1)
s2 = [item + 2 ** (n - 1) for item in s1[::-1]]
s1.extend(s2) return s1
#Leet Code# Gray Code的更多相关文章
- 89. Gray Code(中等,了解啥是 gray code)
知道啥是 gray code 就是收获了. 下面介绍了 gray code 发明的 motivation, 了解动机后就知道啥是 gray code 了. https://zh.wikipedia.o ...
- [LeetCode] Gray Code 格雷码
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- 【LeetCode】Gray Code
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
- Gray Code
Gray Code The gray code is a binary numeral system where two successive values differ in only one bi ...
- 【leetcode】Gray Code (middle)
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 ...
- 44. Decode Ways && Gray Code
Decode Ways A message containing letters from A-Z is being encoded to numbers using the following ma ...
- LeetCode——Gray Code
Description: The gray code is a binary numeral system where two successive values differ in only one ...
- LeetCode:Gray Code(格雷码)
题目链接 The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...
随机推荐
- php输出错误信息
error_reporting(E_ALL); ini_set('display_errors','on'); header("Content-Type:text/html;charset ...
- 【转】在XCode工程中创建bundle包
http://www.giser.net/?p=859 Bundle Programming Guide: https://developer.apple.com/library/ios/docume ...
- 20 Best Drag and Drop jQuery Plugins--reference
reference from:http://dizyne.net/20-best-drag-drop-jquery-plugins/ jQuery has done a great job repla ...
- Hibernate中的"Repeated column in mapping for entity"异常
转:http://lijiejava.iteye.com/blog/786535 一对多双向关联(类Item与类Bid): Item类: public class Item { private int ...
- oracle中对LONG列进行查询
SQL> CREATE TABLE T_LONG (ID NUMBER, LONG_COL LONG); 表已创建. SQL> INSERT INTO T_LONG VALUES (1, ...
- ASP.NET 不同页面之间传值
不同页面之间如何传值?我们假设A和B两个页面,A是传递,B是接收. 下面学习4种方式: 通过URL链接地址传递 POST方式传递 Session方式传递 Application方式传递 1. 通过UR ...
- Azure PowerShell 创建虚拟机
# 指定订阅名称$subscriptionName="订阅名称"# 指定云服务名称$serviceName="云服务名称"# 指定用来保存虚拟机VHD的存储$s ...
- 我的第二篇--nginx安装问题之路径问题
这几天还是一直在搭建nginx,并且要在nginx的基础之上配置naxsi(WAF防火墙)并使它生效,但是随之而来的问题也会有很多,也许因为我是个新手,所以遇到的问题要多,不解的问题也要很多,不知道又 ...
- [YII]将ar查询结果转化成数组
$Column= Column::model()->findAll(); $data = array_map(create_function('$m', 'return $m->getAt ...
- 使用SQL循环打印'*'菱形
菱形每一行都是由n个' ' + n 个'**' + 1个'*' 组成的 例如高度为9的菱形(共print 9次),*最多的一次print为第五次第五次就是0个' ' + 4个'**' + 1个'*' ...