当前位置:网站首页>LeetCode 201. 数字范围按位与
LeetCode 201. 数字范围按位与
2022-06-13 09:03:00 【Shao_sen】
201. 数字范围按位与
难度 中等
给你两个整数 left 和 right ,表示区间 [left, right] ,返回此区间内所有数字 按位与 的结果(包含 left 、right 端点)。
示例 1:
输入:left = 5, right = 7
输出:4
示例 2:
输入:left = 0, right = 0
输出:0
示例 3:
输入:left = 1, right = 2147483647
输出:0
提示:
0 <= left <= right <= 231 - 1
题解
刚开始看到的时候,就觉得是数学题目,就去看看有什么规律,然后果然找到一个规律,就是如果left右移一位还小于right的话,一定返回0。(简称跨位与)
| 2^3 | 2^2 | 2^1 | 2^0 | |
|---|---|---|---|---|
| 5 | 0 | 1 | 0 | 1 |
| 6 | 0 | 1 | 1 | 0 |
| 7 | 0 | 1 | 1 | 1 |
| 8 | 1 | 0 | 0 | 0 |
| 9 | 1 | 0 | 0 | 1 |
当5 & 6 & 7 = 4时,4的二进制位100
| 2^3 | 2^2 | 2^1 | 2^0 | |
|---|---|---|---|---|
| 4 | 0 | 1 | 0 | 0 |
| 8 | 1 | 0 | 0 | 0 |
| 9 | 1 | 0 | 0 | 1 |
4 & 8 = 0,很容易就发现,如果right(9)大于left(5)左移一位(right > left << 1)时,无论后面是什么,返回的肯定是0。
另外可能要注意的就是left左移的时候会溢出(变为负数),导致right一定大于left << 1,所以我们还需要判断left是否大于2^30。
如果大于,那就会溢出,但是right小于2^31-1,两个条件限制了left和right最高位一样,不会出现跨位与的情况;
如果小于,不会溢出,再判断是否出现跨位与
class Solution {
public int rangeBitwiseAnd(int left, int right) {
if(left < (1 << 30) && right > (left << 1)){
//left是否小于1 << 30,且判断是否出现跨位与
return 0;
}else{
//没有出现跨位与,直接返回按位与的情况
int ans = left;
while(left < right){
ans &= ++left;
}
return ans;
}
}
}
移位
| 2^3 | 2^2 | 2^1 | 2^0 | |
|---|---|---|---|---|
| 5 | 0 | 1 | 0 | 1 |
| 6 | 0 | 1 | 1 | 0 |
| 7 | 0 | 1 | 1 | 1 |
| 8 | 1 | 0 | 0 | 0 |
| 9 | 1 | 0 | 0 | 1 |
还是这个表格,我们可以发现,当5 & 6 & 7 的时候,返回的是4,是他们的公共前缀,他们高位都是一样的,然后1 * 2^2 = 4。当我们5 & 6 & 7 & 8 & 9的时候,返回的是0,因为公共前缀在5,6,7出现了0。
所有我们要求他们的公共前缀,那名字高效的求解公共前缀呢,那就是移位了,通过左移,当左移到同一个数值时,就停止,比如5跟7左移两位变成1,8跟9左移两位变成1。然后我们统计左移了多少位,再右移回去,就得到了想要的答案,比如5跟7左移两位变为1之后,1右移两位变成4,。
class Solution {
public int rangeBitwiseAnd(int left, int right) {
int shift = 0;//统计移动了多少位
while(left < right){
//会一直移动到right == left或者right和left都为0
left >>= 1;
right >>= 1;
++shift;
}
return left << shift;//右移复原
}
}
边栏推荐
- Use of grep
- C language time difference calculation
- 线上调试工具Arthas高级
- CAS无锁
- C/s model and P2P model
- 20211028 Stabilizability
- JUC atomic reference and ABA problem
- 你不知道的stringstream的用法
- Library management system based on wechat applet Rar (thesis + source code)
- Tutorial (5.0) 04 Fortint cloud services and scripts * fortiedr * Fortinet network security expert NSE 5
猜你喜欢

How many TCP connections can a machine create at most?

网络安全漏洞分析之重定向漏洞分析

Tutorial (5.0) 01 Product introduction and installation * fortiedr * Fortinet network security expert NSE 5

How to become a white hat hacker? I suggest you start from these stages

Use of grep

教程篇(5.0) 04. Fortint云服务和脚本 * FortiEDR * Fortinet 网络安全专家 NSE 5

【网络安全】SQL注入新思维之webshell提权

Cmake Learning Series I

Collection of garbled code problems in idea development environment

20211020 academician all drive system
随机推荐
20211018 一些特殊矩阵
20211104 为什么矩阵的迹等于特征值之和,为什么矩阵的行列式等于特征值之积
JUC原子引用与ABA问题
20220524 如何把CoppeliaSim安装到D盘
Library management system based on wechat applet Rar (thesis + source code)
How many TCP connections can a machine create at most?
20220606 关于矩阵的Young不等式
An error CV2 is reported when the picture is converted to grayscale cvtColor(img, cv2.COLOR_BGR2GRAY)
教程篇(5.0) 03. 安全策略 * FortiEDR * Fortinet 网络安全专家 NSE 5
The Jenkins console does not output custom shell execution logs
How excel adds hyperlinks to some text in a cell
Message Oriented Middleware
20211006 integral, differential and projection belong to linear transformation
20211104 为什么相似矩阵的迹相同
20211115 矩阵对角化的充要条件;满秩矩阵不一定有n个线性无关的特征向量;对称矩阵一定可以对角化
20211006 线性变换
Yolov5 face video stream
BGP 联邦+Community
20211108 微分跟踪器
Overview of common layers of image recognition neural network (under update)