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的更多相关文章

  1. [codility] Lession1 - Iterations - BinaryGap

    Task1: A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is ...

  2. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  3. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  4. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  5. 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 ...

  6. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  7. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  8. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

  9. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

随机推荐

  1. MySQl的group by 与排序

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtoAAAFLCAIAAABnXrwfAAAgAElEQVR4nO2dT29jvZHu+WHnW8xqdl ...

  2. Nginx禁止域名恶意解析

    今天打开网站发现访客人数突增啊,不对啊,小站哪来这么多的访问量呢?打开百度统计,看到有其他的域名解析到我的IP,心中很不爽啊.遂搜索之,才有了此篇文章. 打开Nginx配置文件/etc/nginx/s ...

  3. 浅谈js数据类型识别方法

    js有6种基本数据类型  Undefined , Null , Boolean , Number , String ,Symbol和一种引用类型Object,下面我们就来一一看穿,哦不,识别他们. t ...

  4. 多进程回声服务器/客户端【linux】

    并发服务器端 #include <unistd.h> #include <stdio.h> #include <sys/wait.h> #include <c ...

  5. 【51nod-1315】合法整数集(数位)

    [思路] 既然是or操作,将数转化为二进制,数位是1,对应的数组元素+1,再将x转为成二进制,只要查找X为1的位置,将之前存放的数组数字找个最小的输出就可以了. 但是并不是所有的数都要参与or,因为有 ...

  6. eureka -2 - 重要配置

    Server 端配置 eureka.client.registerWithEureka :是否将自己注册到Eureka Server,默认是true,如果是单节点部署,切是server端,则设置成fa ...

  7. python爬虫之性能相关

    性能相关 在编写爬虫时,性能的消耗主要在IO请求中,当单进程单线程模式下请求URL时必然会引起等待,从而使得请求整体变慢. import requests def fetch_async(url): ...

  8. PHP工作笔记:离线执行php任务

    直接上代码,主要函数 ignore_user_abort(true);这个函数忽略了终端被关闭(打开的网页被关闭),后面 getfiles()这函数是执行采集任务的自定义函数,后面又配置了下路径打开写 ...

  9. Linux:finger命令详解

    finger 用于查找并显示用户信息 包括本地与远端主机的用户皆可,帐号名称没有大小写的差别. 单独执行finger指令,它会显示本地主机现在所有的用户的登陆信息,包括帐号名称,真实姓名,登入终端机, ...

  10. PHP错误Parse error: syntax error, unexpected end of file in test.php on line 12解决方法

    出现这个错误的原因就是语法错误,肯定是PHP程序的书写不规范造成,PHP语句标识符错了,没有在php.ini中开启短标签!八成是这个原因,啊啊啊! 今天在写PHP程序的时候总是出现这样的错误:Pars ...