Iterations --codility
lesson 1:Iterations
1. BinaryGap-----[100%]
Find longest sequence of zeros in binary representation of an integer.
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.
解题思路:
- 将整型数据转为二进制,然后利用python的split函数按‘1’分割,再用max函数找到最大的切片即可
- 注意二进制为全1,或者第一位为1,后面全为0的数,e.g. 1, 100, 11111, 用log次的循环判定下即可
- 满足于O(log(N))的时间复杂度
from math import log
def solution(N):
# write your code in Python 2.7
#if N == 1: #can belong to the next for i = 0
# return 0
for i in xrange(int(log(N,2))+1):
tmp = 2**i
#print tmp
if tmp == N+1 or tmp == N:
return 0
strb = bin(N).split("1")
#print strb[1:-1]
#last one can't included. e.g.1001000->2, so [1:-1]
if strb[1:-1] is None:
#print "test"
return 0
return max([len(l) for l in strb[1:-1]])
Iterations --codility的更多相关文章
- [codility] Lession1 - Iterations - BinaryGap
Task1: A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is ...
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- Minimum no. of iterations to pass information to all nodes in the tree
Given a very large n-ary tree. Where the root node has some information which it wants to pass to al ...
- *[codility]Peaks
https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...
- *[codility]Country network
https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...
- *[codility]AscendingPaths
https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...
- *[codility]MaxDoubleSliceSum
https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...
随机推荐
- 关于mybatis mapper.xml中的if判断
场景: 页面上有搜索框进行调节查询,不同搜索框中的内容可以为空. 过程: 点击搜索,前端把参数传给后台,这是后台要把为空的参数过滤掉. 做法: 通常我们在dao层即mapper.xml中进行过滤判断操 ...
- yii2出现的400错误
来一段百度来的正常解决方法,注意有很大的坑! 第一种解决办法是关闭Csrf 1配置文件关闭 2控制器里面关闭 public function init(){ $this->enableCsrfV ...
- 【Error】Creating Server TCP listening socket *:6379: bind: No such file or directory
redis 运行服务时报错: Creating Server TCP listening socket *:6379: bind: No such file or directory 解决方法,依次输 ...
- ansible入门一(Ansible介绍及安装部署)
本节内容: 运维工具 Ansible特性 Ansible架构图和核心组件 安装Ansible 演示使用示例 一.运维工具 作为一个Linux运维人员,需要了解大量的运维工具,并熟知这些工具的差异,能够 ...
- 安装使用babel-polyfill。让IE支持es6
安装 npm install --save-dev babel-polyfill 使用 在你的代码头部加载babel-polyfill,注意一定要在你的代码开始前,第一个js文件的顶部.如果是vue在 ...
- C++:获取指定目录下的所有文件
1.获得指定目录下的所有文件(不搜索子文件夹) 需要包含的头文件 #include <io.h> #include <string> #include <vector&g ...
- 二十三、DBMS_METADATA(提供提取数据库对象的完整定义的接口)
1.概述 作用:提供提取数据库对象的完整定义的接口.这些定义可以用XML或SQL DDL格式描述.提供两种类型接口:可编程控制的接口:用于Ad Hoc查询的简单接口. 2.包的组成 dbms_meta ...
- eureka -1 - 介绍
eureka ,服务发现注册中心 eureka 包含server, client两部分. eureka server,服务发现组件,各个微服务启动的时候会向server注册自己的信息(ip,hostn ...
- pg_bulkload使用记录
很久之前就使用过pg_bulkload来导入数据了,并做了对比试验,现在另一个项目又需要用了,这里做个记录: 1.rpm包比较老,下下来之后发现只支持到pg94,目前我用的是pg10,因此放弃. 2. ...
- flask 文件的上传下载和excel操作
文件的下载 from flask import send_from_directory @excel_bp.route('/get_attachment/<path:filename>') ...