1.求整数最大的连续0的个数 BinaryGap Find longest sequence of zeros in binary representation of an integer.
求整数最大的连续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.的更多相关文章
- (笔试题)N!尾部连续0的个数
题目: 对任意输入的正整数N,编写C程序求N!的尾部连续0的个数,并指出计算复杂度.如:18!=6402373705728000,尾部连续0的个数是3. (不用考虑数值超出计算机整数界限的问题) 思路 ...
- Light oj 1138 - Trailing Zeroes (III) 【二分查找 && N!中末尾连续0的个数】
1138 - Trailing Zeroes (III) problem=1138"> problem=1138&language=english&type=pdf&q ...
- C语言中的位操作(16)--计算二进制数字尾部连续0的数目
本篇文章介绍计算二进制数字尾部连续0的数目的相关算法,例如:v=(1101000)2,该数尾部连续0的数目=3 方法1:线性时间算法 unsigned int v; // 需要计算的目标整数 int ...
- (笔试题)N!的三进制数尾部0的个数
题目: 用十进制计算30!(30的阶乘),将结果转换成3进制进行表示的话,该进制下的结果末尾会有____个0. 思路: 这道题与上一篇博文N!尾部连续0的个数的思路是一样的. 计算N!下三进制结果末尾 ...
- 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 ...
- noi 04:求整数的和与均值
04:求整数的和与均值 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 读入n(1 <= n <= 10000)个整数,求它们的和与均值. 输入 ...
- 团体程序设计天梯赛-练习集L1-008. 求整数段和
L1-008. 求整数段和 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 杨起帆 给定两个整数A和B,输出从A到B的所有整数以及这些 ...
- IO-03. 求整数均值
/** *A3-IO-03. 求整数均值(10) *C语言实现 *测试已通过 */ #include "stdio.h" int main() { int a,s,d,f; sca ...
- Algorithm --> 求阶乘末尾0的个数
求阶乘末尾0的个数 (1)给定一个整数N,那么N的阶乘N!末尾有多少个0?比如:N=10,N!=3628800,N!的末尾有2个0. (2)求N!的二进制表示中最低位为1的位置. 第一题 考虑哪些数相 ...
随机推荐
- error: no such device : 76de62ec-ac60-4c4d-bb Entering rescue mode .. grub resuce>(系统硬盘驱动器MBR已损坏)问题解决办法(图文详解)
问题详情 近期,由于博主我,担任实验室整个大数据集群的leader,突然的断电给整个集群造成,如下的情况问题.(欲哭无泪,我的各种服务啊) 解决办法 第一种方法:尝试,直接重启机器(我这里是台式机 ...
- Snipaste强大离线/在线截屏软件的下载、安装和使用
步骤一: https://zh.snipaste.com/ ,去此官网下载. 步骤二:由于此是个绿色软件,直接解压即可. 步骤三:使用,见官网.ttps://zh.snipaste.com 按F1 ...
- centos 7 中防火墙的关闭问题
新安装的centos 7 发现有些程序端口是关闭的,想到了防火墙和selinux selinx 好关闭 /etc/sysconfig/selinux 中 追加 SELINUX=disabled 防火 ...
- KM算法(Kuhn-Munkres)
算法理论基础: 可行顶点标号 用l(v)表示顶点v的标号,w(uv)表示边(u,v)的权,对于赋权二分图G=(X,Y),若对每条边e=xy,均有l(x)+l(y)>=w(xy),则称这个标号为G ...
- C# 字符串每隔两个 提取
private void button3_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); str ...
- nginx_gzip压缩提升网站的传输速度
gzip on; gzip_min_length 1k; gzip_buffers 16k; #gzip_http_version 1.0; gzip_comp_level ; gzip_types ...
- 10Oracle Database 数据表数据查询
Oracle Database 数据表数据查询 DML 数据操纵语言 - 数据的查看和维护 select / insert /delete /update 基本查询语句 Select [distinc ...
- JavaScript中的方法
JavaScript中的方法 在JavaScript中,可以通过对象来调用对应的方法.在JavaScript中,有三个重要的window对象方法:用于显示警告信息的alert.用于显示确认信息的con ...
- Gitlab forbidden
搭建使用了两年的gitlab 抽风了居然forbidden # vim /etc/gitlab/gitlab.rb gitlab_rails['rack_attack_git_basic_auth'] ...
- Object.prototype 原型和原型链
Object.prototype 原型和原型链 原型 Javascript中所有的对象都是Object的实例,并继承Object.prototype的属性和方法,有些属性是隐藏的.换句话说,在对象创建 ...