Task1:

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.

Write a function:

    function solution($N);

that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.

For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5.

Assume that:

    N is an integer within the range [1..2,147,483,647].

Complexity:

    expected worst-case time complexity is O(log(N));
expected worst-case space complexity is O(1).

My Solution in PHP:

// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n"; function solution($N) {
$binary = decbin($N);
$ret = 0; // echo $binary . "\n"; // 1111 situation
// 0100 situation
if ( ( false === strpos($binary, '0') )
|| ( stripos($binary, '1') == strrpos($binary, '1') )
) {
// 默认 $ret = 0 的情况
} else { $cnt = strlen($binary);
$idx = null; for ($i = 0; $i < $cnt; $i++) {
if ($binary[$i] == 1) {
if ( is_null($idx) ) {
// 第一个idx记下来
$idx = $i;
} else {
// 第二个开始计算距离
$tmp = $i - $idx - 1;
$ret = ($tmp > $ret) ? $tmp : $ret;
// 把当前$i记为上一次1出现时的idx
$idx = $i;
}
}
}
} return $ret;
}

Lession1: https://codility.com/programmers/lessons/1-iterations/

Results: https://codility.com/demo/results/trainingU2ACFM-7PE/

Link: http://www.cnblogs.com/farwish/p/6664049.html

[codility] Lession1 - Iterations - BinaryGap的更多相关文章

  1. Iterations --codility

    lesson 1:Iterations 1. BinaryGap-----[100%] Find longest sequence of zeros in binary representation ...

  2. codility上的练习 (1)

    codility上面添加了教程.目前只有lesson 1,讲复杂度的……里面有几个题, 目前感觉题库的题简单. tasks: Frog-Jmp: 一只青蛙,要从X跳到Y或者大于等于Y的地方,每次跳的距 ...

  3. Codility NumberSolitaire Solution

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

  4. codility flags solution

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

  5. GenomicRangeQuery /codility/ preFix sums

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

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

  7. *[codility]Peaks

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

  8. *[codility]Country network

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

  9. *[codility]AscendingPaths

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

随机推荐

  1. ECJTUACM16 Winter vacation training #5 题解&源码

    A------------------------------------------------------------------------------------------- 题目链接:ht ...

  2. Java面向对象抽象类案例分析

    /** 雇员示例: 需求:公司中程序员有姓名,工号,薪水,工作内容 项目经理除了有姓名,工号,薪水还有奖金,工作内容 对给出需求进行数据建模 分析: 在这个问题领域中,先找出涉及的对象 通过名词提炼法 ...

  3. HDU-5157Harry and magic string

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5157 先从后往前插点,在构造回文树时,让cnt[i]+=cnt[fail[i]],然后维护一个后缀和a. ...

  4. Linux使用Public Key方式远程登录

    一.前言: ssh远程登录密码认证的方式有三种,password.Keyboard Interactive.Public Key 前面两种方式就是密码认证,含义都是一样大同小异.第三种是登录方式最安全 ...

  5. oracle创建触发器及作用举例

    --创建触发器及作用举例 create or replace trigger tri before delete on emp --在删除emp表数据之前需要做的事根据自己的业务去写,before是在 ...

  6. 无序列表li横向排列的间隙问题

    今天在写页面的时候,无意中遇到这样一个问题,就是无序列表li横向排列即做成导航栏时,列表项间有间隙. 如: 将列表项li变成列表块后(即将li标签设置为,display:inline-block后), ...

  7. Linux 离线安装Rubygems详解

    很多时候我们会发现,真实的生成环境很多都没有外网,只有内网环境,这个时候我们又需要安装RubyGems,则不能提供yum命令进行在线安装了,这个时候我们就需要下载安装包进行离线安装.本文主要简单介绍如 ...

  8. hbase性能优化总结

    hbase性能优化总结 1. 表的设计 1.1 Pre-Creating Regions 默认情况下,在创建HBase表的时候会自动创建一个region分区,当导入数据的时候,所有的HBase客户端都 ...

  9. jsp页面固定页面为绝对路径

    1 <!-- 固定到绝对路径 --> 2 <base href="<%=request.getContextPath()%>/"/>

  10. windows下安装redis3.2.100单机和集群详解

    下载redis 下载地址:https://github.com/MicrosoftArchive/redis/releases 我下载的是3.2.100版本的Redis-x64-3.2.100.zip ...