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. Swagger的简单入门【转载】

    一.Swagger简介 上一篇文章中我们介绍了Spring Boot对Restful的支持,这篇文章我们继续讨论这个话题,不过,我们这里不再讨论Restful API如何实现,而是讨论Restful ...

  2. Redis4.0 Cluster — Centos7

    本文版权归博客园和作者吴双本人共同所有 转载和爬虫请注明原文地址 www.cnblogs.com/tdws 一.基础安装 wget http://download.redis.io/releases/ ...

  3. [bzoj3673] 可持久化并查集 by zky

    总感觉到现在才来写这题有点奇怪. 并查集如果按秩合并的话,每次合并只会修改一个点的父亲. 用可持久化线段树来实现可持久化数组就行了.. 然而我写的是按子树大小合并..结果比按秩合并慢了一点>_& ...

  4. 判断标签是否包含class的方法

    if ($(this).find('i').hasClass('l-icon-wuxing')) { //取消收藏 $(this).find('i').removeClass('l-icon-wuxi ...

  5. javaScript事件流是什么?

    一.事件 事件是文档或者浏览器窗口中发生的,特定的交互瞬间. 事件是用户或浏览器自身执行的某种动作,如click,load和mouseover都是事件的名字. 事件是javaScript和DOM之间交 ...

  6. nginx 防火墙、权限问题

      1.nginx安装,配置完成之后,尝试访问没有响应,主机可以ping通,/var/log/nginx/access.log日志没有查到任何记录 解决方法:查看linux防火墙,关闭 命令:ipta ...

  7. tp5 url 线上访问 在nginx 上 出现404错误,解决办法(1.80nginx 配置 pathInfo)

      对于ThinkPHP的URL访问路劲如:http://域名/index.php/Index/BlogTest/read,原先的Nginx的是不支持的pathinfo路劲的,导致你在thinkPHP ...

  8. [知了堂学习笔记]_记一次BootStrap的使用

    效果图如下: 一.简介: 什么是Bootstrap?  Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架. 什么是响应式布局? 引用一句Bootstrap的标题语 " ...

  9. java基础复习1

    jre:Java运行环境 jdk:Java开发工具(包含jre) java两大机制:JVM (java虚拟机) 垃圾回收 变量的分类: 1.按数据类型分: 1)基本数据类型:8种 整型:byte sh ...

  10. python3 第十七章 - sequence(序列)

    之前我们在讲for循环语句时就提到过序列,那么什么是序列(sequence)? 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 —— 它的索引(位置),第一个索引是0,第二个索引 ...