[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
[codility] Lession1 - Iterations - BinaryGap的更多相关文章
- Iterations --codility
lesson 1:Iterations 1. BinaryGap-----[100%] Find longest sequence of zeros in binary representation ...
- codility上的练习 (1)
codility上面添加了教程.目前只有lesson 1,讲复杂度的……里面有几个题, 目前感觉题库的题简单. tasks: Frog-Jmp: 一只青蛙,要从X跳到Y或者大于等于Y的地方,每次跳的距 ...
- 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,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...
随机推荐
- NPOI操作Excel 踩坑记
1 读取Excel并修改单元格 a.一定不能一边读数据,一边修改单元格,否则读出来的数据可能不准 b.注意写文件的模式,不然修改后的文件,打开会报错. c.清空单元格的数据,可以调用SetCellTy ...
- hihoCoder #1053 : 居民迁移(贪心,二分搜索,google在线技术笔试模拟)
#1053 : 居民迁移 时间限制:3000ms 单点时限:1000ms 内存限制:256MB 描述 公元2411年,人类开始在地球以外的行星建立居住点.在第1326号殖民星上,N个居住点分布在一条直 ...
- HDU1016 DFS+回溯(保存路径)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- RMQ算法
1. 概述 RMQ(Range Minimum/Maximum Query),即区间最值查询,是指这样一个问题:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A ...
- Travelling(spfa+状态压缩dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 Travelling Time Limit: 6000/3000 MS (Java/Others ...
- Spring框架学习笔记(2)——IOC&DI
IOC:控制反转,创建对象的权利交给Spring,Spring会自动创建对象. DI:依赖注入,操作的对象靠Spring注入,如果不使用Spring,对象的属性值是要靠setter方法来添加的,使用S ...
- centos7 hue安装
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- RAID 详解
一.什么是RAID 磁盘阵列全名是『Redundant Arrays of Inexpensive Disks, RAID 』,英翻中的意思是:容错式廉价磁盘阵列. RAID 可以透过一个技术(软件或 ...
- SAX解析原理示意
SAX解析原理示意
- linux安装navicat全程记录
国庆期间自己在试着用linux(ubuntu),献上navicat安装方法,以及很多教程里没有写的一些小东西 step1: 去navicat官网下载安装包,网址:http://www.navicat. ...