[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,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...
随机推荐
- Redis与数据库同步问题
缓存数据与持久化数据的一致性,这个问题总结了一下(看到了一个不错的博文),其实就是读和写,还有就是要注意谁先谁后的问题. Redis 是一个高性能的key-value数据库. redis的出现,很大程 ...
- eclipse自动提示功能没了的解决方法
eclipse没有自动提示功能,也就是当一个对象居然点不出他的相关方法.后来网上搜索了下,成功的 办法是. 1.我window->Preferences->Java->Editor- ...
- Java多线程与并发编程学习
一.线程三大特性 多线程有三大特性,原子性.可见性.有序性 1.1 什么是原子性 即一个操作或者多个操作 要么全部执行并且执行的过程不会被任何因素打断,要么就都不执行.一个很经典的例子就是银行账户转账 ...
- Promises-小程序购物车结算
//结算提交 checkOut : function(){ var price = this.data.sum; var user = wx.getStorageSync('userInfo'); i ...
- LightOJ DNA Prefix(字典树+dfs)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121897#problem/F F - DNA Prefix Time Limit:200 ...
- Saving James Bond(dijk)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1245 Saving James Bond Time Limit: 6000/3000 MS (Java ...
- JSON对象添加删除属性
假如目前我们有如下一个Json对象 var jsonObj={ 'param1':22, 'param2' :33 }; 增加属性: 我们现在向该对象jsonObj中添加一个新的属性字段:param3 ...
- Windows 部署 Redis 群集(转)
1,下载Redis for windows 的最新版本,解压到 c:\Redis 目录下备用https://github.com/MSOpenTech/redis/releases当前我使用的是 3. ...
- oracle存储过程的创建和使用
创建存储过程: 格式:create or replace procedure procedure_name(参数 参数类型) Is/as 变量1 变量1的类型: begin ----------业务逻 ...
- Spark应用_PageView_UserView_HotChannel
Spark应用_PageView_UserView_HotChannel 一.PV 对某一个页面的访问量,在页面中进行刷新一次就是一次pv PV {p1, (u1,u2,u3,u1,u2,u4-)} ...