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. Python之坐标轴刻度细化、坐标轴设置、标题图例添加

    学习python中matplotlib绘图设置坐标轴刻度.文本 http://www.jb51.net/article/134638.htm Python绘图 https://www.cnblogs. ...

  2. C# - Generics泛型,一图话c#泛型

    一.一篇好文 https://www.cnblogs.com/yueyue184/p/5032156.html 二.一幅好图

  3. FortiDDoS是使用历史流量基线进行检测的

    Understanding FortiDDoS Detection ModeIn Detection Mode, FortiDDoS logs events and builds traffic st ...

  4. 小练习:Two Sum

    1.example Given nums = [, , , ], target = , Because nums[] + nums[] = + = , , ]. 2.solve class Solut ...

  5. 前端开发必备 - Emmet

    介绍 Emmet (前身为 Zen Coding) 是一个能大幅度提高前端开发效率的一个工具. 基本上,大多数的文本编辑器都会允许你存储和重用一些代码块,我们称之为"片段".虽然片 ...

  6. 轮播图插件 SuperSlide2.1滑动门jQuery插件

    http://down.admin5.com/demo/code_pop/18/562/ SuperSlide2.1滑动门jQuery插件

  7. web版源码管理软件SCM-Manager安装配置

    背景 一直使用 “VisualSvn Server” 作为源码管理工具,使用一段时间之后,使用场景遇到以下问题 添加用户必需登录到服务器. 一台服务器,只能安装一个 “VisualSvn Server ...

  8. spring --解析自定义注解SpringAOP(配合@Aspect)

    1:首先,声明自定义注解 @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public @interface DtT ...

  9. 【Sizzle学习】之关于【初探 jQuery 的 Sizzle 选择器】这篇文章里的小bug

    [题记]不可否认,这篇文章写得非常好,但是今天我在看sizzle源码的时候,发现这文章有一地方说的不妥.重现:当selectors为"p.class1>p.class2",j ...

  10. New Concept English there (7)

    27w/m Has it ever happened to you? Have you ever put your trousers in the washing machine and then r ...