$obj
9/2
4 1
2 0
1 0
0 1
529
264 1
132 0
66 0
33 0
16 1
8 0
4 0
2 0
1 0
0 1
32
16 0
8 0
4 0
2 0
1 0
0 1
数据结构:栈
练习题
A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.
Write a function:
def solution(N)
that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.
For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..2,147,483,647].
def solution(N):
def conversionOfNumberSystemsFromDecimalism(decimalismNum, base):
stackContainer = []
N = decimalismNum
b = base
start = N
while True:
m = start % b
start = int(start / b)
stackContainer.append(m)
if start == 0:
break
return stackContainer stackContainer = conversionOfNumberSystemsFromDecimalism(N, 2)
if stackContainer.count(1) <= 1:
return 0
else:
l = stackContainer[stackContainer.index(1):]
s = ''.join([str(i) for i in l])
return max([len(i) for i in s.split('1')])
随机推荐
- iOS 使用动态库
苹果的开放态度 WWDC2014上发布的Xcode6 beta版有了不少更新,其中令我惊讶的一个是苹果在iOS上开放了动态库,在Xcode6 Beta版的更新文档中是这样描述的: Frameworks ...
- 【代码审计】iZhanCMS_v2.1 后台任意文件删除漏洞分析
0x00 环境准备 iZhanCMS官网:http://www.izhancms.com 网站源码版本:爱站CMS(zend6.0) V2.1 程序源码下载:http://www.izhancms ...
- Unity关闭shader中的光照模型以及如何自定义光照模型
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject' // Upgrade NOTE: replaced '_Wor ...
- 解决16bit压缩贴图失真问题
选择索引模式
- pyinstaller 将.py生成.exe ----报错 “IndexError: tuple index out of range”
pyinstaller将py打包为exe文件,用pysintaller居然报错 File "c:\anaconda3\lib\site-packages\PyInstaller\depend ...
- XML的基本用法
一.概述 XML全称为可扩展的标记语言.主要用于描述数据和用作配置文件. XML文档在逻辑上主要由一下5个部分组成: XML声明:指明所用XML的版本.文档的编码.文档的独立性信息 文档类型声明:指出 ...
- Swift - 类型转换(as as! as?)
swift 类型转换 一,as 1,as使用场合 (1)从派生类转换为基类,向上转型(upcasts) class Animal {} class Cat: Animal {} let cat = C ...
- open-falcon之query
功能 query组件,提供统一的绘图数据查询入口.query组件接收查询请求,根据一致性哈希算法去相应的graph实例查询不同metric的数据,然后汇总拿到的数据,最后统一返回给用户. 配置文件 { ...
- c++ vector详解
容器有太多需要清楚细节的地方,同时也有太多值得学习的地方.下文作为学习.工作中用到vector的总结. 1. 赋值运算=的实现原理 在使用赋值操作时,如果不清楚内部是怎么实现,那么用起来会畏手畏脚. ...
- Android Security Internals