给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离。

如果没有两个连续的 1,返回 0 。

示例 1:

输入:22 输出:2 解释: 22 的二进制是 0b10110 。 在 22 的二进制表示中,有三个 1,组成两对连续的 1 。 第一对连续的 1 中,两个 1 之间的距离为 2 。 第二对连续的 1 中,两个 1 之间的距离为 1 。 答案取两个距离之中最大的,也就是 2 。

示例 2:

输入:5 输出:2 解释: 5 的二进制是 0b101 。

示例 3:

输入:6 输出:1 解释: 6 的二进制是 0b110 。

示例 4:

输入:8 输出:0 解释: 8 的二进制是 0b1000 。 在 8 的二进制表示中没有连续的 1,所以返回 0 。

提示:

  • 1 <= N <= 10^9
class Solution {
public:
int binaryGap(int N) {
if(N <= 1)
return 0;
vector<int> bits;
while(N)
{
int temp = N % 2;
bits.push_back(temp);
N /= 2;
}
int len = bits.size();
int first = -1;
int second = -1;
int MAX = 0;
for(int i = 0; i < len; i++)
{
if(bits[i] == 1)
{
if(first == -1)
{
first = i;
}
else if(second = -1)
{
second = i;
MAX = max(MAX, second - first);
second = -1;
first = i;
}
}
}
return MAX;
}
};

Leetcode868.Binary Gap二进制间距的更多相关文章

  1. [LeetCode] Binary Gap 二进制间隙

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

  2. Binary Gap(二进制空白)

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

  3. Leetcode 868. 二进制间距

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

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

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

  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. [Swift]LeetCode868. 二进制间距 | Binary Gap

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

  8. [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数

    Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...

  9. 762. Prime Number of Set Bits in Binary Representation二进制中有质数个1的数量

    [抄题]: Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...

随机推荐

  1. Luogu P3007 [USACO11JAN]大陆议会The Continental Cowngress

    P3007 [USACO11JAN]大陆议会The Continental Cowngress 题意 题意翻译 简述:给出\(n\)个法案,\(m\)头牛的意见,每头牛有两个表决格式为"支持 ...

  2. JSOI 2016 扭动的字符串

    JSOI 2016 扭动的字符串 题面描述 给出两个长度为\(n\)的字符串\(A,B\) \(S(i,j,k)\)表示把\(A\)中的\([i,j]\)和\(B\)中的\([j,k]\)拼接起来的字 ...

  3. object and namespace

    http://effbot.org/zone/python-objects.htm 几点总结: (1) 类的基本属性 . id, returned by id(obj) . type, returne ...

  4. SQL的特点

    1.综合统一 2.高度非过程化 3.面向集合的操作方式 4.一同一种语法结构提供两种使用5.语言简洁易学易用

  5. java填坑记录

    一.The absolute uri: [http://java.sun.com/jsp/jstl/core] cannot be resolved in either web.xml or the ...

  6. 自定义切面实现用户日志记录--AOP

    在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的 ...

  7. Maven中央仓库地址大全,Maven中央仓库配置示例

    < Maven 中央仓库地址大全 > 在上一篇文章中完成了 < Maven镜像地址大全 >,后来又花了时间又去收集并整理了关于 maven 远程仓库地址,并整理于此,关于 Ma ...

  8. tensorflow高效地推导pb模型,完整代码

    from matplotlib import pyplot as plt import numpy as np import os import six.moves.urllib as urllib ...

  9. virtualenv简单使用

    前提 在开发过程中,经常需要使用各种第三方库,而且python又提供了pip,easy_install等工具来简化库的安装,所以很容易就会在系统python的site-packages目录中装满各种各 ...

  10. vue项目中实现扫码功能

    项目地址:https://github.com/wkl007/vue-scan-demo.git 项目主要是做的一个扫码的功能 核心代码为 <div class="scan" ...