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.还有最后 ...
随机推荐
- Linux命令详解-pwd
Linux中用 pwd 命令来查看"当前工作目录"的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判定当前目录在文 ...
- jmeter-02 JMeter 生成HTML性能报告
Report Dashboard: JMeter3.0 后提供的扩展模块,支持从测试计划中获取图形和统计数据,生成HTML页面格式图形化报告. 快速入门演示 一.准备测试计划 mock_api .jm ...
- wordcount程序出现map 100% reduce 0%问题的解决方法
运行wordcount程序一直停在map 100% reduce 0%, input文件夹的内容: 其中: f1.txt中的内容为:hello hadoop f2.txt中的内容为:hello had ...
- Java环境搭建---(基础)
首先下载eclipse开发工具,下载地址:http://www.eclipse.org/downloads/,界面如下: 选择eclipse juno(4.2)的版本进入界面 点击Downloads, ...
- 032——VUE中表单控件处理之复选框的处理
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MongoHelper.cs
using System; using MongoDB.Bson; using MongoDB; using System.Web; using MongoDB.Driver; namespace Y ...
- 缓存LruCache简单创建和使用
LruCache一般使用: /** * 总容量为当前进程的1/8,单位:KB * sizeOf():计算缓存对象的大小,单位要一致 * entryRemoved():移除旧缓存时调用 */ int m ...
- React之前端路由
通过之前的博客介绍,对于react,我们已经可以写单个组件.复合组件/单个页面了,接下来就是实现页面的跳转了,这个时候,我们就需要前端路由了. 一.react-router-dom 安装这个依赖,th ...
- Bandit:一种简单而强大的在线学习算法
假设我有5枚硬币,都是正反面不均匀的.我们玩一个游戏,每次你可以选择其中一枚硬币掷出,如果掷出正面,你将得到一百块奖励.掷硬币的次数有限(比如10000次),显然,如果要拿到最多的利益,你要做的就是尽 ...
- SQL基础五(作业代码)
create database stuinfo create table student ( mid ) not null primary key, mname ) not null ) create ...