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. 第九天 1-8 RHEL7软件包管理

    在RHEL7中,主要有 RPM 和 YUM 两种包管理 1.RPM包管理--使用rpm命令对rpm软件包进行管理rpm命令格式:[有很多,自己可以man一下,这里只列举一下常用的]rpm -ivh [ ...

  2. web前端 —— 移动端知识的一些总结

    个人在移动端的一些总结归纳,有新的知识点会一直更新 一.css部分 1.meta标签 <meta name="viewport" content="width=de ...

  3. Ansible 小手册系列 五(inventory 主机清单)

    Ansible 可同时操作属于一个组的多台主机,组和主机之间的关系通过 inventory 文件配置. 默认的文件路径为 /etc/ansible/hosts 主机清单示例 mail.example. ...

  4. 010——数组(十)compact extract in_array

    <?php /** 10 数组 compact extract in_array */ //compact() (紧凑的,简洁的) 将变量转换为数组,变量名为数组键名,变量值为数组的键值. /* ...

  5. MySQL,SqlServer数据库关键字在程序中处理

    这个原来是SqlServer的数据库,现在改成MySQL的,由于两个数据库有些差别.在程序中怎么处理.为了给自己提个醒,把它记录下来. 这是MySQL数据库 Public Sub display() ...

  6. hdu 5826 physics (物理数学,积分)

    physics Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  7. JS中apply与call的用法

    JavaScript中有一个call和apply方法,其作用基本相同,但也有略微的区别. 先来看看JS手册中对call的解释: call 方法调用一个对象的一个方法,以另一个对象替换当前对象. cal ...

  8. MATLAB基础知识tips

    网络论坛资源: MATLAB中文论坛https://i.cnblogs.com/EditPosts.aspx?opt=1 circshift 移位函数,circshift(A,SHIFTSIZE).S ...

  9. Git远程操作详解(转)

    转自:http://www.ruanyifeng.com/blog/2014/06/git_remote.html Git远程操作详解   Git是目前最流行的版本管理系统,学会Git几乎成了开发者的 ...

  10. (一) ffmpeg filter学习-使用流程

    FFMPEG中有一个类库:libavfilter.该类库提供了各种视音频过滤器.之前一直没有怎么使用过这个类库,最近看了一下它的使用说明,发现还是很强大的,有很多现成的filter供使用,完成视频的处 ...