给定一个正整数 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. C语言中常用的字符串处理函数总结

    C语言中字符串处理函数备注 此文仅用于自己研究和记录 字符串处理函数 1. char *gets(char *s); #include<stdio.h> 功能: 从标准输入读入字符,并保存 ...

  2. TZ_03_mybatis的xml开发

    1.通过Student.xml编写sql来操作数据库 1>insert语句插入后返回主键 加入标签useGeneratedKeys=“true” keyProperty=“oid” 中 keyP ...

  3. Node.js学习(Node.js基础)

    1.开发环境搭建 v8引擎是作为解析JavaScript程序来运行的 nodejs是一个高性能的,第一个体现在他的JavaScript解析速度很快,v8引擎性能很高,第二个事件驱动和非阻塞 2.全局对 ...

  4. jeecms获取绝对路径

    jeecms后台管理做一个附件上传到服务器上,然后读取改上传文件,半天获取不到路径,后来发现有定义好的绝对路径获取方法: //最好将文件上传到u文件夹底下 String path="/u/c ...

  5. 使用em为单位制作两列弹性布局

    一.DIV布局按照定位的方法分为:浮动方法(float),坐标定位方法(position),还有就是两者相结合的方法. 二.DIV布局按照定义单位的不同可分为:固定宽度布局.流体布局.弹性布局和混合布 ...

  6. 【DM642学习笔记三】flash的烧写

    ICETEK-DM642-PCI板上的29L008B芯片提供了8M位的Flash空间(访问地址空间是CE1,90000000h~90080000h).主要用于自启动功能和存储FPGA的配置数据. 一. ...

  7. python-基础-练习和面试题

    给程序传参数 import sys print(sys.argv) 运行结果: 列表推导式 所谓的列表推导式,就是指的轻量级循环创建列表 1. 基本的方式 2. 在循环的过程中使用if 3. 2个fo ...

  8. Linux User and Group Management

    linux is a multi-user and multitasking OS. In Linux, you can create any number of user account and g ...

  9. windows搭建rabbitmq ha

    1.安装erlang22.0 rabbitmq 3.7.15 2.bin下执行命令:rabbitmq-plugins enable rabbitmq_management3.替换.erlang.coo ...

  10. TCP/TP:DNS区域(Zone)

    之前阅读资料不是特别明白,看到一个博主的解释,豁然开朗,特此记录. https://blog.csdn.net/huangzx3/article/details/79347556 DNS区域(ZONE ...