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. Dubbo本地开发技巧

    背景 作为后端服务负载.前后分离的主要手段,dubbo在业界中使用率还比较高.随着Dubbo系统的增多,本地开发.调试就出现了麻烦之处 直接在开发本地起同样一份服务 由于Dubbo采用负载均衡的策略, ...

  2. day31 堡垒机尾声 + Python与金融量化分析(一)

    堡垒机尾声: 代码案例:https://github.com/liyongsan/git_class/tree/master/day31 课堂笔记:file send: 1.选择本地文件 2.远程路径 ...

  3. Day11 - Python操作memcache、redis缓存、rabbitMQ队列

    本周课前必备: 1. Memcached 2. Python操作Memcached模块: https://pypi.python.org/pypi/python-memcached 3. Redis ...

  4. 微信小程序单个倒计时效果

    var end_time = grouponList.expire_time.replace(/-/g, '/') grouponcountdown(that, end_time) //适用于商品列表 ...

  5. CF991C

    题解: 很显然不会有那么多种肯能 所以都列出来即可 代码: #include<bits/stdc++.h> using namespace std; int main() { ]; sca ...

  6. win10下安装VS2005运行程序出现0x000007b错误的解决方法

    项目工程一运行就报错...真心坑... 方法如下: 1.安装DirectX 9.0c 形成原因是因为DirectX 9.0被损坏, 只需要安装即可. 如果有电脑管家的.在电脑管家里面搜索“Direct ...

  7. Linux环境安装xmapp(PHP-Mysql集成环境)

    xmapp是很多初学者使用的PHP环境集成包,用yum在linux安装的php和mysql版本现在都很低,xmapp可以解决这个问题,下面我们直接进入正题,安装一个php集成环境. 我使用的是vm虚拟 ...

  8. python装饰器中functools.wraps的作用详解

    直接上代码看效果: # 定义一个最简单的装饰器 def user_login_data(f): def wrapper(*args, **kwargs): return f(*args, **kwar ...

  9. Java集合体系总结

    一.集合框架 集合是容纳数据的容器,java常用的集合体系图如下.以集合中是否运行重复元素来分,主要有List和Set接口,List集合中可以有重复元素,Set集合集合中的元素不可重复,Iterato ...

  10. Amazon面试题

    亚马逊面试题: 如下所示的Map中,0代表海水,1代表岛屿,其中每一个岛屿与其八领域的区间的小岛能相连组成岛屿群.写代码,统计Map中岛屿个数. /* Q1. Map [ 0 0 0 0 0 0 0 ...