求整数最大的连续0的个数 

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).
package com.codility;
public class Test01 {
public int solution(int N) {
if(N<=0){
return 0;
}
String str = Integer.toBinaryString(N);
int size = str.length();
int count=0;
int max = 0;
for(int i=0;i<size;i++){
String str01 = str.substring(i, i+1);
if(str01.equals("1")){
if(count>0){
max = Math.max(count, max);
}
count = 0;
}
if(str01.equals("0")){
count ++;
}
}
return max;
}
public static void main(String[] args) {
String str = Integer.toBinaryString(20);
System.out.println(str);
// System.out.println(str.substring(0, 1));
Test01 t01 = new Test01();
System.out.println(t01.solution(5));
System.out.println(t01.solution(20));
System.out.println(t01.solution(529));
}
}

1.求整数最大的连续0的个数 BinaryGap Find longest sequence of zeros in binary representation of an integer.的更多相关文章

  1. (笔试题)N!尾部连续0的个数

    题目: 对任意输入的正整数N,编写C程序求N!的尾部连续0的个数,并指出计算复杂度.如:18!=6402373705728000,尾部连续0的个数是3. (不用考虑数值超出计算机整数界限的问题) 思路 ...

  2. Light oj 1138 - Trailing Zeroes (III) 【二分查找 &amp;&amp; N!中末尾连续0的个数】

    1138 - Trailing Zeroes (III) problem=1138"> problem=1138&language=english&type=pdf&q ...

  3. C语言中的位操作(16)--计算二进制数字尾部连续0的数目

    本篇文章介绍计算二进制数字尾部连续0的数目的相关算法,例如:v=(1101000)2,该数尾部连续0的数目=3 方法1:线性时间算法 unsigned int v; // 需要计算的目标整数 int ...

  4. (笔试题)N!的三进制数尾部0的个数

    题目: 用十进制计算30!(30的阶乘),将结果转换成3进制进行表示的话,该进制下的结果末尾会有____个0. 思路: 这道题与上一篇博文N!尾部连续0的个数的思路是一样的. 计算N!下三进制结果末尾 ...

  5. CodeForces 527C. Glass Carving (SBT,线段树,set,最长连续0)

    原题地址:http://codeforces.com/problemset/problem/527/C Examples input H V V V output input H V V H V ou ...

  6. noi 04:求整数的和与均值

    04:求整数的和与均值 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 读入n(1 <= n <= 10000)个整数,求它们的和与均值. 输入 ...

  7. 团体程序设计天梯赛-练习集L1-008. 求整数段和

    L1-008. 求整数段和 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 杨起帆 给定两个整数A和B,输出从A到B的所有整数以及这些 ...

  8. IO-03. 求整数均值

    /** *A3-IO-03. 求整数均值(10) *C语言实现 *测试已通过 */ #include "stdio.h" int main() { int a,s,d,f; sca ...

  9. Algorithm --> 求阶乘末尾0的个数

    求阶乘末尾0的个数 (1)给定一个整数N,那么N的阶乘N!末尾有多少个0?比如:N=10,N!=3628800,N!的末尾有2个0. (2)求N!的二进制表示中最低位为1的位置. 第一题 考虑哪些数相 ...

随机推荐

  1. selenium-server 启动命令

    启动hub主机: java -jar selenium-server-standalone-2.39.0.jar -role hub 启动node 本地:java -jar selenium-serv ...

  2. python2 'str' object has no attribute 'decode'

    '.decode('hex') 上述代码,报错: 'str' object has no attribute 'decode' 查找原因: https://stackoverflow.com/ques ...

  3. Generating RSA keys in PKCS#1 format in Java--转

    原文地址:https://stackoverflow.com/questions/7611383/generating-rsa-keys-in-pkcs1-format-in-java When I ...

  4. document.write清除原有内容情况

    原博客: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...

  5. socket相关函数

    socket() 我们使用系统调用socket()来获得文件描述符:#include<sys/types.h>#include<sys/socket.h>int socket( ...

  6. 2星|《约见投资人》:A股上市公司软文集

    约见资本人:58家上市公司创始人亲述创业之路 全书写了58个A股上市公司的故事,基本是宣传上市公司老总的软文.基本的套路是创始人历尽苦难创立了公司,取得了好业绩.最希望看的分析与数据几乎没有.看了一小 ...

  7. Hive DDL&DML

    1.删除分区 ALTER TABLE table_name DROP IF EXISTS PARTITION(dt=') 如果是外部表,记得rm对应文件 2.添加分区 ALTER TABLE tabl ...

  8. jmeter接口测试小结

    摘自:http://www.cnblogs.com/houzhizhe/p/6839736.html JMeter做http接口压力测试 测前准备 用JMeter做接口的压测非常方便,在压测之前我们需 ...

  9. vue(数据改变,DOM不渲染问题)

    1.组件内部,属性值地址空间内引用地址改变,DOM不能渲染. 问题举例:this.items = [[],[],[],[]] 1.在items 中,修改任意一项数组中的值,DOM是不会更新的,2.解决 ...

  10. 梦想CAD控件网页版关于自定义命令

    在CAD控件操作中,为方便使用者,使用自定义命令发出命令,完成CAD绘图,修改,保存等操作.点击此处在线演示. _DMxDrawX::RegistUserCustomCommand 向CAD控件注册一 ...