这是悦乐书的第333次更新,第357篇原创

01看题和准备

今天介绍的是LeetCode算法题中Easy级别的第203题(顺位题号是868)。给定正整数N,找到并返回N的二进制表示中两个连续1之间的最长距离。如果没有连续两个1,则返回0。例如:

输入:22

输出:2

说明:22的二进制是10110。在22的二进制表示中,有三个1,第一对连续的1距离为2,第二对1的距离为1,答案是这两个距离中最大的一个,即2。



输入:5

输出:2

说明:5的二进制是101。



输入:6

输出:1

说明:6的二进制是110。



输入:8

输出:0

说明:8的二进制是1000。在二进制表示为8时没有任何连续的1,所以我们返回0。

注意

  • 1 <= N <= 10 ^ 9

02 第一种解法

题目的意思是计算一个二进制数中每对1的最长距离,若是只有一个1,距离则为0。

因此,我们先将N转为一个二进制字符串,然后去遍历字符串中的字符,使用一个临时变量存储前一个1的索引值,遇到新的1时就计算彼此之间的距离,取最大值,最后输出距离。

public int binaryGap(int N) {
String str = Integer.toBinaryString(N);
int prev = -1, distance = 0;
for (int i=0; i<str.length(); i++) {
if (str.charAt(i) == '1') {
if (prev == -1) {
prev = i;
} else {
distance = Math.max(distance, i-prev);
prev = i;
}
}
}
return distance;
}

03 第二种解法

如果不借助包装类Integer的转换二进制字符串方法,还有其他方式可以解吗?

可以,使用位运算即可。

使用右移位运算,可以得到其二进制数最后一位数,判断是1还是0,可以使用与运算,与运算的规则是相同位上为1就为1,0&1 = 0,1&1=1。剩下的就是计算最长距离了,还是使用一个临时变量存储前一次的1,最后输出最长距离。

public int binaryGap(int N) {
int prev = -1, distance = 0;
for (int i=0; i<32; i++) {
if (((N>>i)&1) == 1) {
if (prev != -1) {
distance = Math.max(distance, i-prev);
}
prev = i;
}
}
return distance;
}

04 第三种解法

和上面第二种解法一样的思路,只是将右移位运算、与运算拆分成两步来完成,循环也换成了while循环,其他处理思路没变。

public int binaryGap(int N) {
int prev = -1, distance = 0, i = 0;
while (N != 0) {
if ((N&1) == 1) {
if (prev != -1) {
distance = Math.max(distance, i-prev);
}
prev = i;
}
i++;
N >>= 1;
}
return distance;
}

05 小结

算法专题目前已连续日更超过六个月,算法题文章203+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

LeetCode.868-二进制距离(Binary Gap)的更多相关文章

  1. Leetcode 868. 二进制间距

    868. 二进制间距  显示英文描述 我的提交返回竞赛   用户通过次数201 用户尝试次数220 通过次数207 提交次数396 题目难度Easy 给定一个正整数 N,找到并返回 N 的二进制表示中 ...

  2. [Swift]LeetCode868. 二进制间距 | Binary Gap

    Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...

  3. Binary Gap(二进制空白)

    中文标题[二进制空白] 英文描述 A binary gap within a positive integer N is any maximal sequence of consecutive zer ...

  4. # Leetcode 67:Add Binary(二进制求和)

    Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...

  5. 【Leetcode_easy】868. Binary Gap

    problem 868. Binary Gap solution1: class Solution { public: int binaryGap(int N) { ; vector<int&g ...

  6. [leetcode] (周赛)868. 二进制间距

    868. 二进制间距 读懂题意就出来了 class Solution { public int binaryGap(int N) { String s = Integer.toBinaryString ...

  7. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  8. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  9. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  10. LeetCode:Maximum Depth of Binary Tree_104

    LeetCode:Maximum Depth of Binary Tree [问题再现] Given a binary tree, find its maximum depth. The maximu ...

随机推荐

  1. InterlliJ idea文件夹里面无法新建java文件等

    这两天开始实习,因为公司用的InterlliJ idea作为开发工具,所以我这两天也开始学习如何使用这个.所以想将在操作中遇到的问题做笔记发表到上面来.也方便自己随时查阅,也希望能帮助到正在阅读的你! ...

  2. centos7使用cron任务的相关命令(与centos6有区别)

    一.cron任务的自启动相关命令 1.检测cron定时服务是否自启用 systemctl is-enabled crond.service 结果展示如下: enable表示已启用自启动 disable ...

  3. PHP对有道翻译API函数

    首先需要先到http://fanyi.youdao.com/openapi?path=data-mode申请APIKEY.然后公布代码: <?php header("Content-T ...

  4. spring AspectJ的Execution表达式(转载)

    原文:http://blog.csdn.net/peng658890/article/details/7223046 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需 ...

  5. UI控件Telerik UI for ASP.NET MVC全新发布R2 2019 SP1

    Telerik UI for ASP.NET MVC拥有使用JavaScript和HTML5构建网站和移动应用所需的70+UI部件,来满足开发者的各种需求,提供无语伦比的开发性能和用户体验.它主要是针 ...

  6. Linux系统中的硬件问题如何排查?(5)

    Linux系统中的硬件问题如何排查?(5) 2013-03-27 10:32 核子可乐译 51CTO.com 字号:T | T 在Linux系统中,对于硬件故障问题的排查可能是计算机管理领域最棘手的工 ...

  7. Keras class_weight和sample_weight用法

    搬运: https://stackoverflow.com/questions/57610804/when-is-the-timing-to-use-sample-weights-in-keras i ...

  8. Python 函数Ⅱ

    以上代码中,[1,2,3] 是 List 类型,"Runoob" 是 String 类型,而变量 a 是没有类型,她仅仅是一个对象的引用(一个指针),可以是 List 类型对象,也 ...

  9. Linux 查看内存(free)、释放内存(基本操作)

    原文链接:http://blog.51cto.com/11495268/2384147 1.简介 1.1 介绍 很多时候,服务器 负载 很高(执行操作 很慢),很多 原因 造成 这种 现象(内存不足 ...

  10. 【BZOJ4570】 [Scoi2016]妖怪

    Description 邱老师是妖怪爱好者,他有n只妖怪,每只妖怪有攻击力atk和防御力dnf两种属性.邱老师立志成为妖怪大师,于 是他从真新镇出发,踏上未知的旅途,见识不同的风景.环境对妖怪的战斗力 ...