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

If there aren't two consecutive 1's, return 0.

Example 1:

Input: 22
Output: 2
Explanation:
22 in binary is 0b10110.
In the binary representation of 22, there are three ones, and two consecutive pairs of 1's.
The first consecutive pair of 1's have distance 2.
The second consecutive pair of 1's have distance 1.
The answer is the largest of these two distances, which is 2.

Example 2:

Input: 5
Output: 2
Explanation:
5 in binary is 0b101.

Example 3:

Input: 6
Output: 1
Explanation:
6 in binary is 0b110.

Example 4:

Input: 8
Output: 0
Explanation:
8 in binary is 0b1000.
There aren't any consecutive pairs of 1's in the binary representation of 8, so we return 0.

Note:

  • 1 <= N <= 10^9

这道题给了我们一个正整数,问其二进制表示数中两个 '1' 之间的最大距离是多少。比如整数 22 的二进制为 10110,那么可以看出前两个 '1' 之间的距离最大,所以返回2即可。其实这道题的考察点就是如何按位取数,对一个二进制数直接与 '1',就可以提取出最低位,然后和0比较大小,就知道最低位是0还是1了。当然为了每一位的是0还是1,一般有两种处理方法,一种是直接将原数字右移一位,则之前的最低位被移除了,再和 '1' 相与即可,直到原数字变为0停止。另一种方法是直接右移固定的位数再与 '1',因为整型数只有 32 位,所以可以直接取出任意位上的数字。那么既然要求两个 '1' 之间的最大距离,那么只要关心 '1' 的位置即可,一种比较直接的思路就是先遍历一遍各位上的数字,将所有 '1' 的坐标位置都保存到一个数组 pos 之中,然后再遍历这个 pos 数组,计算相邻两个数字的差,即两个 '1' 之间的距离,更新结果 res 即可得到最大距离,参见代码如下:


解法一:

class Solution {
public:
int binaryGap(int N) {
vector<int> pos;
for (int i = 0; i < 32; ++i) {
if (((N >> i) & 1) != 0) pos.push_back(i);
}
int res = 0, n = pos.size();
for (int i = 1; i < n; ++i) {
res = max(res, pos[i] - pos[i - 1]);
}
return res;
}
};

我们也可以只用一个循环来完成,而且不用 pos 数组,只用一个变量 last 来记录上一个遍历到的 '1' 的位置,初始化为 -1。那么在遍历的过程中,若遇到了 '1',则判断 last 是否大于等于0,是的话则表示之前已经遇到了 '1',那么当前位置i减去 last,即为两个 '1' 之间的距离,用来更新结果 res,然后再更新 last 为当前位置i,继续遍历即可,参见代码如下:


解法二:

class Solution {
public:
int binaryGap(int N) {
int res = 0, last = -1;
for (int i = 0; i < 32; ++i) {
if (((N >> i) & 1) != 0) {
if (last >= 0) res = max(res, i - last);
last = i;
}
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/868

参考资料:

https://leetcode.com/problems/binary-gap/

https://leetcode.com/problems/binary-gap/discuss/149945/Simple-Java-(10-ms)

https://leetcode.com/problems/binary-gap/discuss/149835/C%2B%2BJavaPython-Dividing-by-2

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] Binary Gap 二进制间隙的更多相关文章

  1. Leetcode868.Binary Gap二进制间距

    给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离. 如果没有两个连续的 1,返回 0 . 示例 1: 输入:22 输出:2 解释: 22 的二进制是 0b10110 . ...

  2. Binary Gap(二进制空白)

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

  3. LeetCode & Binary Search 解题模版

    LeetCode & Binary Search 解题模版 In computer science, binary search, also known as half-interval se ...

  4. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  5. LeetCode: Binary Tree Traversal

    LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...

  6. [LeetCode] Binary Search 二分搜索法

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...

  7. 【LeetCode-面试算法经典-Java实现】【067-Add Binary(二进制加法)】

    [067-Add Binary(二进制加法)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given two binary strings, return thei ...

  8. 【Leetcode_easy】868. Binary Gap

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

  9. LeetCode Binary Search All In One

    LeetCode Binary Search All In One Binary Search 二分查找算法 https://leetcode-cn.com/problems/binary-searc ...

随机推荐

  1. 二值化神经网络(BNN)基础学习(一)

    目录 1.简介 2.优点 3.基本原理 3.1 权重和激活值二值化[3] 3.2 乘法优化 3.3 权重和激活值更新 4.结论[3] 参考资料 1.简介 ​ 二值化神经网络,在浮点型(权重值和激活函数 ...

  2. day09 详解内存管理机制

    """ 今日内容:详解内存管理 1.引用计数 在内存中为了对变量的值进行标记从而方便管理,采用引用计数的方式对变量进行标记. (1)如果变量的值被引用一次,那么该变量的引 ...

  3. dos.orm的事务处理

    dos.orm也包含事务处理,没有太多封装,这里有几个简单的示例代码. using (DbTrans trans = DbSession.Default.BeginTransaction()) { D ...

  4. python3 切片

    一.切片的目的:获取多个元素 能够进行切片的对象有:字符串.列表.元组 语法:  object[start_index:end_index:step]     以下是创建一个列表 a = [0,1,2 ...

  5. UNIX哲理名言(中英文对照)

    UNIX 的特点: Everything (including hardware) is a file.所有的事物(甚至硬件本身)都是一个的文件. Configuration data stored ...

  6. 论文阅读笔记五十五:DenseBox: Unifying Landmark Localization with End to End Object Detection(CVPR2015)

    论文原址:https://arxiv.org/abs/1509.04874 github:https://github.com/CaptainEven/DenseBox 摘要 本文先提出了一个问题:如 ...

  7. maven 导包报错

    作为初学者本应当是持之以恒的但是很长时间没有冒泡了这次冒个泡写maven项目的时候遇到了很多的bug,今天给大家分享一下解决的办法(常见的错误就是导不进来自己想要的包)要么就是导包报错以下是解决方法 ...

  8. DDD - 概述 - 聚合 - 限界上下文 (四)

    最重要的一句话 DDD的所有有相关理论中,只有一句是至关重要的,但是也是最容易被忽略和最难做到的,抛弃传统的设计方式(思路)的思想,这句话起了决定性的作用,但是99%的人都忽略了或者在开始无法正视或理 ...

  9. Anaconda的安装与使用

    1. 安装Anaconda(Command Line) 1.1 下载 首先去Anaconda官网查看下载链接,然后通过命令行下载: $ wget https://repo.anaconda.com/a ...

  10. 8、socket以及socketserver

    Python 提供了两个基本的 socket 模块.第一个是 Socket,它提供了标准的 BSD Sockets API.第二个是 SocketServer, 它提供了服务器中心类,可以简化网络服务 ...