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

数据结构:栈

练习题

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')])

  

随机推荐

  1. api 25 PopupWindow会占据整个屏幕

    解决方法:if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // Android 7.x中,PopupWindow高度为match_pa ...

  2. spring boot配置文件application.propertis

    转自http://www.qiyadeng.com/post/spring-boot-application-properties 本文记录Spring Boot application.proper ...

  3. 微信小程序 禁止ios页面下拉下滑滚动 出现空白的情况

    项目需要做了一个图片拖动指定组件上删除,和排序的功能android测试正常, ios会出现拖动图片页面也跟着下滑的尴尬情况. 查文档下拉刷新配置默认是关闭的,后经查找文档发现在本页面page.json ...

  4. codeforces水题100道 第十四题 Codeforces Round #321 (Div. 2) A. Kefa and First Steps (brute force)

    题目链接:http://www.codeforces.com/problemset/problem/580/A题意:求最长连续非降子序列的长度.C++代码: #include <iostream ...

  5. 剑指offer面试题6:重建二叉树

    1.题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树. public class Solution { public TreeNode reConstructBinaryTree(int ...

  6. springbatch---->springbatch的使用(五)

    这里我们介绍一个从数据库读取数据并写入到文件中的案例.如果能真心爱上一个人,那么不管对方是何等恶劣,哪怕对方并不爱自己,人生也至少不会是地狱,就算多少有点黯淡. 读取数据库数据 一.定义一个读写的jo ...

  7. 【linux系列】centos安装vsftp

    一.检查vsftpd软件 如果发现上不了网可以修改配置文件中的ONBOOT=no改为yes,然后重启服务试试

  8. iPhone 上如何通过 Safari 使用 Pocket

     在开始之前,请确认你的机器上已经安装了 Pocket  应用软件. 如何安装 1.打开Pocket应用,点击左上角的菜单(三条横岗),找到最下面的 Help ,点击 How To Save ,找到 ...

  9. 0R的电阻以及NC的意义

    0欧电阻的作用: 0欧的电阻大概有以下几个功能:①做为跳线使用.这样既美观,安装也方便.②在数字和模拟等混合电路中,往往要求两个地分开,并且单点连接.我们可以用一个0欧的电阻来连接这两个地,而不是直接 ...

  10. C# 中文日期 周几

    //该语句显示的为英文格式 DateTime.Now.DayOfWeek.ToString(); //显示中文格式星期几 "星期" + DateTime.Now.ToString( ...