Task description

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:

class Solution { public int solution(int 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).
 
Solution

 
Programming language used: Java
Total time used: 8 minutes
Code: 09:57:26 UTC, java, final, score:  100
// you can also use imports, for example:
// import java.util.*; // you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message"); class Solution {
public int solution(int N) {
// write your code in Java SE 8
int longest=0,temp=0;
int test = N;
boolean power = false;
if(test % 2 == 0){
test = test / 2;
power = true;
}
while(test !=0) {
if(test % 2 ==0) {
if(power) {
test = test / 2;
continue;
}
temp++;
} else {
temp = 0;
power =false;
}
test = test / 2;
if(longest < temp)
longest = temp;
}
return longest;
}
}
 
 

Codility---BinaryGap的更多相关文章

  1. [codility] Lession1 - Iterations - BinaryGap

    Task1: A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is ...

  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. *[codility]Peaks

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

  7. *[codility]Country network

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

  8. *[codility]AscendingPaths

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

  9. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

  10. *[codility]Fish

    https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...

随机推荐

  1. [ACM] POJ 1094 Sorting It All Out (拓扑排序)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26801   Accepted: 92 ...

  2. 一段node代码的解读

    path.join(path.dirname(__dirname), platform); __dirname:全局变量,变量获取当前模块文件所在目录的完整绝对路径 path.dirname():返回 ...

  3. Spring+SpringMVC+MyBatis+easyUI

    Spring+SpringMVC+MyBatis+easyUI 日常啰嗦 还好在第一篇文章里就列好了接下来的主线及要写的知识点,不然都不知道要写什么东西了,开篇里已经列了基础篇要讲svn和git的知识 ...

  4. 一个2013届毕业生(踏上IT行业)的迷茫(1)

    从毕业到现在已经快半年了,已经想写这篇文字很久了,但是一次又一次的被没有时间给搁置了,今天突然好想写一篇自己这么多年的总结,算是一种反思,也可以看作为未来的人生指路吧. 我和很多搞IT的同行们一样,不 ...

  5. 【oracle ocp 知识点二】

    1.数据库操作语言 DML在运行时下面的语句 添加一个新行到表 更新表现出一定的线 从表删除现有行 一个事务处理是由一系列的DML语句逻辑组成 A.insert 每次插入一行数据 字符和日期的须要单引 ...

  6. 【Python注意事项】如何理解python中间generator functions和yield表情

    本篇记录自己的笔记Python的generator functions和yield理解表达式. 1. Generator Functions Python支持的generator functions语 ...

  7. Win7使用初体验

    作者:朱金灿 来源:http://blog.csdn.net/clever101 最近装了个Win7旗舰版.随着年纪渐大,我对软件使用越来越趋于务实,不再追求最新版本,因为我评价一个好软件的标准是适用 ...

  8. WPF 同一窗口内的多线程/多进程 UI(使用 SetParent 嵌入另一个窗口)

    原文 WPF 同一窗口内的多线程/多进程 UI(使用 SetParent 嵌入另一个窗口) WPF 的 UI 逻辑只在同一个线程中,这是学习 WPF 开发中大家几乎都会学习到的经验.如果希望做不同线程 ...

  9. C++安全异常std:auto_ptr

    auto_ptr它是C++标准库(<utility>)为了一个智能指针类模板来解决资源泄漏所提供的问题(注意:这只是一个简单的智能指针) auto_ptr在事实原则的实现RAII,对资源的 ...

  10. Go语言的网络功能太强了,这么多项目。。。

    Centrifugo 是一个用 Golang 实现的基于 Websocket 或者 SockJS 的实时通信平台.https://www.oschina.net/p/centrifugalrpcx是一 ...