Codility---BinaryGap
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:
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).
// 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的更多相关文章
- [codility] Lession1 - Iterations - BinaryGap
Task1: A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is ...
- 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 ...
- *[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,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...
- *[codility]MaxDoubleSliceSum
https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...
- *[codility]Fish
https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...
随机推荐
- VS Code插件之Cordova Tools
原文:VS Code插件之Cordova Tools 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u011127019/article/detai ...
- 常见数据结构与算法的 Python 实现
1. 排序 快速排序(quick sort) 形式一:借助 partition 辅助函数 def partition(seq): pivot, seq = seq[0], seq[1:] low = ...
- message contains no documents code:13066 mongdb数据库报的错误
message contains no documents code:13066stackoverflow上面的回答是: What version of the C# driver are you ...
- node lesson5--async
https://github.com/alsotang/node-lessons/tree/master/lesson5 https://github.com/nswbmw/N-blog/wiki/% ...
- 取消Jquery mobile自动省略的文本
在使用jquery moblie做移动客户端app时,listview控件下的列表文本不能完全显示,只能显示一行,超过字数jquery mobile会自动用省略号代替.很是纠结啊. 最后在一个岛国网站 ...
- 还在使用vc6.0吗??vs2010吧
每个人在面对新的事物的时候,总是充满了排斥感,当vista和win7来临的时候,我们还在留恋xp,说xp是如何的好,win7是如何的不给力,当然,我们必须承认,xp是一款优秀的操作系统,这个我们无法否 ...
- python 教程 第九章、 类与面向对象
第九章. 类与面向对象 1) 类 基本类/超类/父类被导出类或子类继承. Inheritance继承 Inheritance is based on attribute lookup in Py ...
- XenServer重置存储管理SR至Ext3
Xenserver默认安装到本地磁盘,它是基于本地磁盘上Linux的LVM所管理,在xenserver创建为LVM Typer的本地SR,xenserver在LVM上不支持Thin Provision ...
- WPF 图形绘制 及各种线帽、箭头的实现
原文:WPF 图形绘制 及各种线帽.箭头的实现 /// <summary> /// 矩形类 /// </summary> public sealed ...
- 说谎的简单工厂模式设计模式&工厂方法模式&Abstract Factory模式
工厂模式被分成三个:简单工厂模式,抽象工厂模式和工厂模式. 定义: 简单工厂模式:用一个单独的类来做创造实例的过程. 工厂模式:一个用于创建对象的接口.让子类决定实例化哪一个类,讲一个类的实例化 延迟 ...