给定一个正整数 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. Vue基础知识梳理

    1. Vue 实例 1.1 创建一个Vue实例 一个 Vue 应用由一个通过 new Vue 创建的根 Vue 实例,以及可选的嵌套的.可复用的组件树组成.demo 1.2 数据与方法 数据的响应式渲 ...

  2. [转]8天玩转并行开发——第二天 Task的使用

    在我们了解Task之前,如果我们要使用多核的功能可能就会自己来开线程,然而这种线程模型在.net 4.0之后被一种称为基于 “任务的编程模型”所冲击,因为task会比thread具有更小的性能开销,不 ...

  3. CF1148F - Foo Fighters

    CF1148F - Foo Fighters 题意:你有n个物品,每个都有val和mask. 你要选择一个数s,如果一个物品的mask & s含有奇数个1,就把val变成-val. 求一个s使 ...

  4. HZOI20190813 B,任(duty)

    题面:去一个神奇的网页:https://www.cnblogs.com/Juve/articles/11352426.html 听说打O(nmq)有70 但是显然博主只有50分 考点:前缀和的综合应用 ...

  5. IDEA设置maven项目的默认配置

    IDEA设置maven项目的默认配置 问题描述 很多刚使用idea的人,用其创建maven工程时会遇到一个问题,明明给项目设置了新的maven配置(使用阿里镜像源或者自定义maven版本),但是重新打 ...

  6. 微信小程序注册使用流程

    新开发微信小程序使用流程:  平时使用小程序较多,但是具体注册流程简单记录下: 第一步:通过邮箱注册 第二步:在邮箱进行激活 注册后,在邮箱会收到激活信息提示.点击激活地址进行激活. 第三步:信息登记 ...

  7. [C#] 生成 (web): 未能加载文件或程序集“Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7

    有时候编译asp.net会遇到奇怪的错误: 生成 (web): 未能加载文件或程序集"Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, ...

  8. 卷积神经网络中的通道 channel

    卷积神经网络中 channels 分为三种:    (1):最初输入的图片样本的 channels ,取决于图片类型,比如RGB, channels=3    (2):卷积操作完成后输出的 out_c ...

  9. Trees in a Wood UVA - 10214 欧拉函数模板

    太坑惹,,,没用longlong各种WA #include <iostream> #include <string.h> #include <cstdio> #in ...

  10. LeetCode412Fizz Buzz

    写一个程序,输出从 1 到 n 数字的字符串表示. 1. 如果 n 是3的倍数,输出"Fizz": 2. 如果 n 是5的倍数,输出"Buzz": 3.如果 n ...