$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')])
随机推荐
- wee hours
前言: 程序员问科比:“你为什么这么成功? ” 科比:“你知道凌晨四点的城市是什么样子吗?” 程序员:“知道,一般那个时候我还在写代码,怎么了?” 科比:“没事了……” 说起程序员,可能很多人脑中会蹦 ...
- 【代码审计】LaySNS_v2.2.0 前台XSS跨站脚本漏洞
0x00 环境准备 LaySNS官网:http://www.laysns.com/ 网站源码版本:LaySNS_v2.2.0 程序源码下载:https://pan.lanzou.com/i0l38 ...
- 管理工具 django-admin.py的相关命令列表
C:\Users\lenovo> django-admin.py Type 'django-admin.py help <subcommand>' for help on a spe ...
- (数字IC)低功耗设计入门(一)——低功耗设计目的与功耗的类型
低功耗设计这个专题整理了好久,有一个月了,有图有证据: 然而最近一直有些烦心事.郁闷事,拖延了一下,虽然现在还是有点烦,但是还是先发表了吧.下面我们就来聊聊低功耗设计吧,由于文章比较长,因此我就不一次 ...
- jquery 动态展示查询条件
<table class="queryTable" width="100%" > <tr> <td class="que ...
- python3 使用matplotlib画图出现中文乱码的情况
python3使用matplotlib画图,因python3默认使用中unicode编码,所以在写代码时不再需要写 plt.xlabel(u’人数’),而是直接写plt.xlabel(‘人数’). 注 ...
- vue案例 - 使用vue实现自定义多选与单选的答题功能
4月底立得flag,五月底插上小旗,结果拖到六月底七月初才来执行.说什么工作忙都是借口,就是睡的比猪早,起的比猪晚. 本来实现多选单选这个功能,vue组件中在表单方面提供了一个v-model指令,非常 ...
- C#TreeView节点选中后失去焦点时改变节点背景色
C#TreeView节点选中后失去焦点时改变节点背景色 在使用TreeView控件时候,单击一个节点,当鼠标聚焦到别的地方的时候,之前点击的这个节点就看不清楚了 举例截图 单击后 ...
- 关于 ArrayList.toArray() 和 Arrays.asList().toArray()方法
1.ArrayList.toArray() 理解 * 通过源码我们可以看到返回的是Object类型的数组,失去了原有的实际类型,虽然底层存储是具体类型的对象,这也正体现了文档中说的:该方法起到了 ...
- [Tjoi2016&Heoi2016]排序[01序列]
4552: [Tjoi2016&Heoi2016]排序 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 994 Solved: 546[Sub ...