[LeetCode] 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
这道题给了我们一个正整数,问其二进制表示数中两个 '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 二进制间隙的更多相关文章
- Leetcode868.Binary Gap二进制间距
给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离. 如果没有两个连续的 1,返回 0 . 示例 1: 输入:22 输出:2 解释: 22 的二进制是 0b10110 . ...
- Binary Gap(二进制空白)
中文标题[二进制空白] 英文描述 A binary gap within a positive integer N is any maximal sequence of consecutive zer ...
- LeetCode & Binary Search 解题模版
LeetCode & Binary Search 解题模版 In computer science, binary search, also known as half-interval se ...
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- LeetCode: Binary Tree Traversal
LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...
- [LeetCode] Binary Search 二分搜索法
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...
- 【LeetCode-面试算法经典-Java实现】【067-Add Binary(二进制加法)】
[067-Add Binary(二进制加法)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given two binary strings, return thei ...
- 【Leetcode_easy】868. Binary Gap
problem 868. Binary Gap solution1: class Solution { public: int binaryGap(int N) { ; vector<int&g ...
- LeetCode Binary Search All In One
LeetCode Binary Search All In One Binary Search 二分查找算法 https://leetcode-cn.com/problems/binary-searc ...
随机推荐
- pythonのdjango 在控制台用log打印操作日志
在Django项目的settings.py文件中,在最后复制粘贴如下代码: LOGGING = { 'version': 1, 'disable_existing_loggers': False, ' ...
- Linux-ubuntu16.04安装 mysql5.7-PHP7.0+Swoole
步骤1 – 安装MySQL 在 Ubuntu 16.04 中,默认情况下,只有最新版本的 MySQL 包含在 APT 软件包存储库中.只需更新服务器上的包索引并安装默认包 apt-get. sudo ...
- 支持向量机SVM——专治线性不可分
SVM原理 线性可分与线性不可分 线性可分 线性不可分-------[无论用哪条直线都无法将女生情绪正确分类] SVM的核函数可以帮助我们: 假设‘开心’是轻飘飘的,“不开心”是沉重的 将三维视图还原 ...
- maven的配置-2019-4-13
一.Maven的优点 1. 依赖管理 jar 包管理 2.一键构建 (编译-----测试------打包-----安装-----部署 ) 什么是项目构建? 指的是项目从编译-----测试------ ...
- cadcam
Email:kefu007@vip.qq.com 13D TIMON 2007 英語版2007 23DVIA Composer V6R2013 中文版2013 3ABQUS V6.11 6.11 4A ...
- java程序设计第二次作业
- 对于JavaBean+Servlet+SqlServer的代码总结和打包调用
日期:2019.3.24 博客期:049 星期日 说起来我已经说过很多次前台的应用技术了呢!这一次我是要将这一部分打包,做成配套的制作工具: 当前我已经打包成功,想要下载的同学可以进入我的GitHub ...
- NAT穿透解决
1.各种网络环境下的P2P通信解决方法: (1)如果通信双方在同一个局域网内,这种情况下可以不借助任何外力直接通过内网地址通信即可: (2)如果通信双方都在有独立的公网地址,这种情况下当然可以不借 ...
- PyQt5目录
记录下学习PyQt5的经过,方便以后查找. Offical website : https://www.riverbankcomputing.com QMainWindow : http://www. ...
- gethostbyname
SYNOPSIS #include <netdb.h> struct hostent *gethostbyname(const char *name); Data Structure ht ...