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. ajax简单手写了一个猜拳游戏

    使用ajax简单写一个猜拳游戏 HTML代码 <!DOCTYPE HTML> <html lang="en-US"> <head> <me ...

  2. android 读书笔记 1

    四层linux 内核层library java 虚拟机frameworkapplication 四组件活动(activity), 服务(service), 广播接收器(broadcast receiv ...

  3. hadoop 随笔

    http://p-x1984.iteye.com/blog/859843 面试hadoop可能被问到的问题,你能回答出几个 ? 1.hadoop运行的原理? 2.mapreduce的原理? 3.HDF ...

  4. [Python] Unofficial Windows Binaries for Python Extension Packages

    1. Unofficial Windows Binaries for Python Extension Packages 非官方的Python第三方库,提供基于Windows的二进制扩展包,由加州大学 ...

  5. flask livereload用法

    #coding=utf-8 from flask import Flask from flask_script import Manager app = Flask(__name__) manager ...

  6. C#实现新建文件并写入内容

    using System; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(stri ...

  7. 如何在浏览器中简单模拟微信浏览器(仅限于通过User Agent进行判断的页面)

    模拟微信浏览器: .打开360极速 .F12开发者工具 .开发者模式左上方有一个手机样子的图标 点击进入 设备模式‘ .将UA选项中的字符串替换成: Mozilla/ 备注: 替换的字符串是微信浏览器 ...

  8. Disk Genius 彻底清理硬盘空闲

  9. 【分享】Linux(Ubuntu)下如何自己编译JDK

    最近在看<深入理解 Java 虚拟机>这本书.里面提到了如何手动编译JDK,于是就试了试. 在编译的过程中,遇到了一些问题.上网一搜,发现了一篇很好的文章,跟大家分享一下:ubuntu 1 ...

  10. MongoDB3.4版本配置详解

    重要配置参数讲解如下 processManagement: fork: <true | false> 描述:是否以fork模式运行mongod/mongos进程,默认为false. pid ...