[codility] Lession1 - Iterations - BinaryGap

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

时间: 2024-10-28 14:33:04

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

Solution to Triangle by Codility

question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one hand, there is no false triangular. Given the array has been sorted, if A[i]+A[i+1]>A[i+2], we can prove the existence of the triangle. for array A i

Solution of Codility

Solution of Codility codility.com is another great place to improve your programming skill. Train myself , and record here. Lesson 1: Time Complexity Lesson 2: Counting Elements Lesson 3: Prefix Sums Lesson 4: Sorting MaxProductOfThree: * Distinct: *

the solution of CountNonDivisible by Codility

question:https://codility.com/programmers/lessons/9 To solve this question , I get each element's divsors which appearing in input Array A using Sieve of Eratosthenes method. Time complexity is O(nlogn); Then  we iterate array A to get the ith non-di

Solution of NumberOfDiscIntersections by Codility

question:https://codility.com/programmers/lessons/4 trap: int overflow code: #include <algorithm> int solution(vector<int> &A) { // write your code in C++11 int size = A.size(); if (size <2) return 0; vector<long> begin; vector<

GenomicRangeQuery /codility/ preFix sums

首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which correspond to the types of successive nucleotides in the sequence. Each nucleotide has an impact factor, which is an integer. Nucleotides of types A,

codility上的问题(34) Fluorum 2014

好久没写codility的题了,一来没时间,二来有的题目不太好分析.这个题比较有意思,我还没有给出非常严格的证明. 给定一棵树(无向无环图),从一个节点出发,每次选择一个节点,从起点到目的节点的路径上没经过的节点尽可能多,直到遍历完所有的节点.如果起点到两个目的节点的路径中没经过的节点同样多,则选择标号较小的节点作为目的节点.如此继续,直到遍历所有的节点,求按顺序选择了哪些目的节点? 例如从2 开始,第一次选择0,然后1,0变为经历过的节点. 然后从0开始,第二次选择6, 然后4,6变为经历过的

codility flags solution

How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers is given. A peak is an array element which is larger than its neighbours. More precisely, it is an index P such that 0 < P < N − 1 and A[P − 1] < A

*[codility]Country network

https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1273 http://blog.csdn.net/caopengcs/article/details/36872627 http://www.quora.com/How-do-I-determine-the-order-of-visiting-all-leave

Codility上的问题(35) Neon 2014

也是比较有意思的题,越来越数学了--不善于做这种题. 如图一个码头有N个木桩,用于拴住船,码头长度是M,可以理解未0到M的线段.有N调船,每条船的一半长度为X,所以船长是2 * X.每个船的中心必须拴在一个木桩上.并且每个木桩只能拴一条船.拴船的绳子长度是船的中心与木桩位置的距离.当然,木桩的位置不能移动,但是船可以自由左右移动.要求船头船尾必须都在码头上(0..M的线段),船也可以看作长度为2 * X的线段,请给每条船指定一个位置让拴船的最长绳子长度最短,求这个最短的绳子长度.如果无法容纳下所