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的位置. 第一题 考虑哪些数相 ...
随机推荐
- Spring.Net学习笔记(1)-容器的使用
一.下载地址: http://www.springframework.net/download.html 二.相关程序集 Spring.Net容器定义在程序集Spring.Core.dll中,它依赖于 ...
- servlet下的request&&response
request的方法 *获取请求方式: request.getMethod(); * 获取ip地址的方法 request.getRemoteAddr(); * 获得用户清气的路 ...
- 01-Entity FrameWork如何控制数据的变化
在Entity Framework所有操作数据就是更新EF容器中的实体状态 public enum EntityState { Added = , Deleted = , Detached = , M ...
- Qt 5.8.3 部署/添加 Crypto++第三方库(5.6.5版本)
首先,Qt没有封装加解密算法库(其实有个哈希函数的函数).介于OpenSSL函数封装不友好,以及先前爆发的心脏滴血漏洞广受诟病,我们考虑在C++上使用一种新的,并且封装友好的,OOAD程度更高的加解密 ...
- 8月中旬之后的学习计划 --- react
这段时间快活了,放纵了,玩hi了,接下来该好好的学习新知识了. 鉴于目前业界比较火的前端js框架有react.vue,决定先从react开始学习.之前有简单的接触过它的一些基本的语法知识,这次准备全面 ...
- JMeter怎样测试WebSocket,示例演示(二)
一.测试案例演示 以 http://www.websocket.org/echo.html 网站为例. 地址为:ws://echo.websocket.org 二.长连接的影响 1.没有勾选stre ...
- 浅谈:nodejs在cmd提示不是内部或外部命令
今天用cmd安装个库,结果发现node不是内部命令,检查后发现上次重装nodejs换了个安装位置,path环境变量忘改了. 找到变量值中node的安装地址,比如C:develop\nodejs,如果不 ...
- ansible结合playbook批量部署war包项目上线
批量部署jenkins.war包实现上线 用于测试war包上线 [root~localhost]~#vim /etc/ansible/test.yml - hosts: test vars: ...
- luogu P3899 [湖南集训]谈笑风生 线段树合并
Code: #include<bits/stdc++.h> #define maxn 300002 #define ll long long using namespace std; vo ...
- java.lang.NoSuchFieldError: DEFAULT_INCOMPATIBLE_IMPROVEMENTS
解决方案: 启动类上加@EnableAutoConfiguration(exclude = { FreeMarkerAutoConfiguration.class }) 或者在配置文件添加spring ...