868. Binary Gap*

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

题目描述

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

C++ 实现 1

使用 bitset 库得到数字 N 表示的二进制数, 用 imax 记录两个连续 1 的最大距离, prev 记录前一个 1 的索引, 初始化为 -1. 遍历二进制数, 如果遇到 1, 先判断前面是否有 1, 判断的标准是 prev 是否为 -1. 如果有, 那么更新距离. 然后更新 prev. 注意 bitsetoperator[] 方法返回的是 bool 值.

class Solution {public:int binaryGap(int N) {std::bitset<32> b(N);int imax = 0, prev = -1;for (int i = 0; i < b.size(); ++ i) {if (b[i]) {if (prev != -1) {imax = std::max(imax, i - prev);}prev = i;}}return imax;}
};

C++ 实现 2

参考 LeetCode 的官方解答 https://leetcode.com/problems/binary-gap/solution/, 如果不使用 bitset, 可以使用右移符以及 & 符, 来得到二进制表达.

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

868. Binary Gap*相关推荐

  1. Leetcode PHP题解--D47 868. Binary Gap

    2019独角兽企业重金招聘Python工程师标准>>> D47 868. Binary Gap 题目链接 868. Binary Gap 题目分析 给定一个数字,计算其二进制表示中, ...

  2. LeetCode: 868. Binary Gap

    LeetCode: 868. Binary Gap 题目描述 Given a positive integer N, find and return the longest distance betw ...

  3. Binary Gap(二进制空白)

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

  4. LeetCode.868-二进制距离(Binary Gap)

    这是悦乐书的第333次更新,第357篇原创 01看题和准备 今天介绍的是LeetCode算法题中Easy级别的第203题(顺位题号是868).给定正整数N,找到并返回N的二进制表示中两个连续1之间的最 ...

  5. LeetCode知识点总结 - 868

    LeetCode 868. Binary Gap 考点 难度 Math Easy 题目 Given a positive integer n, find and return the longest ...

  6. Leetcode算法题-解法转载

    版权声明:本文为博主原创文章,未经博主允许不得转载.    https://blog.csdn.net/fuxuemingzhu/article/details/85112591 作者: 负雪明烛 i ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  8. LeetCode 力扣算法题解汇总,All in One

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: https://fuxuemingzhu.cn 关键词:LeetCode,力扣,算法,题解,汇总,解析 把自己刷过的所有题目做一个整理, ...

  9. CSDN总结的面试中的十大算法

    1.String/Array/Matrix 在Java中,String是一个包含char数组和其它字段.方法的类.如果没有IDE自动完成代码,下面这个方法大家应该记住: toCharArray() / ...

最新文章

  1. SSH协议、HTTPS中SSL协议的完整交互过程
  2. metasploit快速入门(二)收集信息
  3. 使用flex 做关键词、正则表达式过滤
  4. InfoPath表单每增加一个表单产生一个自动增加ID序号
  5. oracle 操作树大全,oracle树查询语句
  6. 档案盒正面标签制作_错题本科学制作方法、正确使用方式及窍门
  7. linux bash命令_Linux命令-您应该知道的基本Bash命令行技巧
  8. 国内滴滴面临“讨伐”,国外Uber日子更难过!活该不?
  9. 数据结构笔记(十七)--矩阵的压缩存储
  10. Linux内核分析:跟踪分析Linux内核的启动过程
  11. 代码雨代码源复制_黑色帝国中代码雨如何实现?用python就可以了
  12. ARM64体系结构编程1-加载与存储指令
  13. 手机签名工具_iOS越狱神器复活!自签工具 ReProvision 又可以愉快使用了
  14. Win7升级Win10系统提示错误0x80070057的解决方法
  15. Debian(Linux) 安装Windows通用字体(可解决TimesNewRoman等字体的报错)
  16. 雅典娜暴利烹饪系列(下)
  17. Mybatis中resultMap和resultType的区别
  18. 测绘——如何在win10环境下安装CAD2006+CASS7.0
  19. vs2012 mvc3项目ObjectContext与DBContext
  20. VR旅游应用案例解析,世界那么大用VR去看看!

热门文章

  1. ansible之判断语句jinja2模板的使用 与roles角色的配置使用
  2. 站内信“数据库设计思路”
  3. 计算机vb里代码里的双引号,在VB中使用字符串中的左双引号
  4. Last packet sent to the server was 2 ms ago 解决办法
  5. JAVA stream流详细教程
  6. 专访百度AI交互设计院院长关岱松:感知类学科的尽头都是心理学 | 甲子光年
  7. 【前端】在vue项目中使用mixpanel记录用户访问量,5s内同一客户端记录一次
  8. 【福利】邀请博主,赢取大奖
  9. 【OI备忘录】dalao博文收藏夹
  10. 【Java】JavaMail发送QQ邮件邮件