中文标题【二进制空白】

英文描述

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. The number 32 has binary representation 100000 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. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [1..2,147,483,647].

中文描述

这里我不按照原文一字一字的翻译,但是尽量按照题目的要求把题目解释清楚。

这里题目的要求是,将 N 为一个整数类型的数据,转换为一个 2 进制的字符串,然后在返回的字符串中返回最大的 0 的间隔数字。

例如 529 转换为 2 进制的字符串为:1000010001,在这里,将会存在以 1 为分割的字符串  0000 和 000,这 2 个字符串的长度分别为 4 和 3。

我们的算法需要返回的值诶 4。

思路和点评

这个题目的思路其实比较简单,你需要首先将 N 这个整数,转换为 0 和 1 的字符串。然后在转换成功的字符串中返回以 1 分分割的 0 的长度。

这里可能需要考虑下面的几种情况。

情况
结果
11 这个情况应该返回的长度为 0
10 这个情况因为没有被 1 这个字符串封闭,因此应该返回长度为 0

传统的思路应该是采取字符串分割的方式,进行遍历后获得结果。

我们在这里采取一种相对不是非常常规的方式,例如在 10000010001 字符串中插入 #,将字符串变为 #1#00000#1#000#1#。

然后将字符串按照 1 进行分割,那么分割后的数组应该分别存储的数据为:#,#0000#,#000#,#

这里我们只需要找到 #...# 中值最大的连续 0 字符串就可以了。基本上可以使用 1 个字符串替换函数和一个字符串分割函数就可以了,并不需要多次存储和遍历。

源代码

源代码和有关代码的更新请访问 GitHub:

https://github.com/cwiki-us/java-tutorial/blob/master/src/test/java/com/ossez/lang/tutorial/tests/codility/CodilityBinaryGapTest.java

代码思路请参考:

package com.ossez.lang.tutorial.tests.codility;import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;/*** <p>* More details about question see link below* <ul>* <li>@see <a href= "https://www.cwiki.us/display/ITCLASSIFICATION/Binary+Gap">https://www.cwiki.us/display/ITCLASSIFICATION/Binary+Gap</a>* </li>* </ul>* </p>* * @author YuCheng**/
public class CodilityBinaryGapTest {private final static Logger logger = LoggerFactory.getLogger(CodilityBinaryGapTest.class);/*** */@Testpublic void testMain() {logger.debug("BEGIN");int N = 529;String intStr = Integer.toBinaryString(N);intStr = intStr.replace("1", "#1#");String[] strArray = intStr.split("1");int maxCount = 0;for (int i = 0; i < strArray.length; i++) {String checkStr = strArray[i];int countLength = 0;if (checkStr.length() > 2 && checkStr.startsWith("#") && checkStr.endsWith("#")) {checkStr = checkStr.replace("#", "");countLength = checkStr.length();if (maxCount < countLength) {maxCount = countLength;}}}logger.debug("MAX COUNT: [{}]", maxCount);}}

https://www.cwiki.us/display/ITCLASSIFICATION/Binary+Gap

Binary Gap(二进制空白)相关推荐

  1. Leetcode PHP题解--D47 868. Binary Gap

    2019独角兽企业重金招聘Python工程师标准>>> D47 868. Binary Gap 题目链接 868. Binary Gap 题目分析 给定一个数字,计算其二进制表示中, ...

  2. 868. Binary Gap*

    868. Binary Gap* https://leetcode.com/problems/binary-gap/ 题目描述 Given a positive integer N, find and ...

  3. LeetCode: 868. Binary Gap

    LeetCode: 868. Binary Gap 题目描述 Given a positive integer N, find and return the longest distance betw ...

  4. JAVA:实现binary exponentiation二进制幂运算算法(附完整源码)

    JAVA:实现binary exponentiation二进制幂运算算法 package com.thealgorithms.divideandconquer;public class BinaryE ...

  5. Binary Bomb 二进制炸弹

    实验报告 实 验(三) 题     目 Binary Bomb 二进制炸弹 计算机科学与技术学院 目  录 第1章 实验基本信息... - 3 - 1.1 实验目的... - 3 - 1.2 实验环境 ...

  6. Binary Watch二进制时间

    [抄题]: A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the b ...

  7. LeetCode.868-二进制距离(Binary Gap)

    这是悦乐书的第333次更新,第357篇原创 01看题和准备 今天介绍的是LeetCode算法题中Easy级别的第203题(顺位题号是868).给定正整数N,找到并返回N的二进制表示中两个连续1之间的最 ...

  8. 数据库binlog(二进制日志binary log)

    二进制日志中存储的内容称之为事件,每一个数据库更新操作(Insert.Update.Delete,不包括Select)等都对应一个事件. mysql binlog基本原理 - 简书 (jianshu. ...

  9. 逻辑航线信息学系列教程:二进制(Binary)

    逻辑航线信息学系列教程 二进制(Binary)         二进制是计算技术中广泛采用的一种数制,是现代计算机的运算基础. 计算机仅仅能识别 "0" 和 "1&quo ...

最新文章

  1. 计算两个日期之间的年数
  2. 恶补各种知识(操作系统篇)
  3. phpcmsV9 关键词keywords删除、替换与修改
  4. [零基础学JAVA]Java SE应用部分-28.Java IO操作(02)
  5. Node 环境变量 process.env.NODE_ENV 之webpack应用
  6. java 中文 转义_java html中文汉字 反转义
  7. 关于jacob支持BSTR类型的经验总结
  8. hadoop集群图解_一个hadoop集群的网络拓补图
  9. webservice接口开发学习笔记(一)
  10. 系统设计之架构图——应用架构图、技术架构图、业务架构图
  11. 如何正确在CSDN问答进行提问
  12. 网络安装CentOS 7
  13. 两例司法实践撕开了一个口子,区块链应用落地会加速吗?
  14. 3dsmax启动闪退的解决方法
  15. 什么是软件验收测试?如何获取软件验收测试报告
  16. 嵌入式软硬件开发区别
  17. python中的_和__
  18. Java实现 蓝桥杯 基因牛的繁殖
  19. 怎么查找计算机里的金蝶账套,金蝶软件 账套如何从一台电脑转到另一台电脑...
  20. Note | 学术论文写作方法和技巧

热门文章

  1. c语言实现定长顺序存储,c语言:定长顺序串的基本操作实实现
  2. tf.keras.layers.Conv2D 示例
  3. UnicodeEncodeError: ‘locale‘ codec can‘t encode character ‘\u5e74‘ in position 2: Illegal byte seque
  4. shell sh: 1: matlab: not found 解决方案
  5. 【node】express的www.js文件里面的process.env.PORT
  6. 树莓派安装Ubuntu
  7. 清华计算机本科高中准备 知乎,什么是清华大学的定向生?清华大学定向生到底要不要报考?(知乎...
  8. c语言界面怎么加图形,「分享」C语言如何编写图形界面
  9. django2 mysql配置_Django:Python3.6.2+Django2.0配置MySQL 转载
  10. pandas describe函数_PANDAS: 新手教程 一