当前位置:网站首页>LeetCode 201. Digit range bitwise AND
LeetCode 201. Digit range bitwise AND
2022-06-13 09:22:00 【Shao_ sen】
201. Digital range bitwise AND
difficulty secondary
Here are two integers left and right , Indicates the interval [left, right] , Returns all numbers in this range Bitwise AND Result ( contain left 、right Endpoint ).
Example 1:
Input :left = 5, right = 7
Output :4
Example 2:
Input :left = 0, right = 0
Output :0
Example 3:
Input :left = 1, right = 2147483647
Output :0
Tips :
0 <= left <= right <= 231 - 1
Answer key
When I first saw it , I think it's a math problem , Just go and see what the rules are , Then I found a rule , Is that if left One shift to the right is less than right Words , Be sure to return 0.( abbreviation Straddle and )
| 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 |
When 5 & 6 & 7 = 4 when ,4 Binary bit of 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, It's easy to find out , If right(9) Greater than left(5) The left one (right > left << 1) when , Whatever's behind it , The return must be 0.
Another thing you may want to pay attention to is left When moving left, it will overflow ( It becomes negative ), Lead to right Must be greater than left << 1, So we still need to judge left Is it greater than 2^30.
If it is greater than , It will overflow , however right Less than 2^31-1, Two conditions limit left and right The highest position is the same , There will be no Straddle and The situation of ;
If it is less than , It won't spill , Then judge whether there is Straddle and
class Solution {
public int rangeBitwiseAnd(int left, int right) {
if(left < (1 << 30) && right > (left << 1)){
//left Is less than 1 << 30, And judge whether there is cross position and
return 0;
}else{
// No straddle and , Return directly to bitwise AND
int ans = left;
while(left < right){
ans &= ++left;
}
return ans;
}
}
}
displacement
| 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 |
Or this form , We can find out , When 5 & 6 & 7 When , The return is 4, Is their public prefix , They are all in the same position , then 1 * 2^2 = 4. When we 5 & 6 & 7 & 8 & 9 When , The return is 0, Because the public prefix is in 5,6,7 There is 0.
All we ask for is their public prefix , How to solve the public prefix efficiently , That's the shift , By moving left , When moving left to the same value , Just stop , such as 5 Follow 7 Move two bits to the left to become 1,8 Follow 9 Move two bits to the left to become 1. Then we count how many bits have been shifted to the left , Move back to the right , You get the answer you want , such as 5 Follow 7 Shift left two bits to 1 after ,1 Move two bits to the right to become 4,.
class Solution {
public int rangeBitwiseAnd(int left, int right) {
int shift = 0;// Count how many bits have been moved
while(left < right){
// Will move all the way to right == left perhaps right and left All for 0
left >>= 1;
right >>= 1;
++shift;
}
return left << shift;// Right shift recovery
}
}
边栏推荐
- The turtle library displays the system time
- Neo4j environment construction
- [implementation of depth first search]
- How Simulink adds modules to the library browser
- Spectre record
- LeetCode 6097. Match after replacing characters (Dictionary)
- turtle库的使用数字时钟模拟时钟动态显示
- Drill down to protobuf - Introduction
- LeetCode 343. 整数拆分
- 虚拟化和云计算文章大合集
猜你喜欢

Class loading overview

Solov2 source code analysis

QT multithreaded TCP server

QObject::connect: Cannot queue arguments of type ‘QTextCursor‘ (Make sure ‘QTextCursor‘ is registere

JUC atomic reference and ABA problem

Storage mode of drawings

IP address introduction

C language: deep understanding of pointers and arrays

C language: deep understanding of character functions and string functions (1)

The turtle library displays the system time
随机推荐
20211018 some special matrices
LeetCode 5259. 计算应缴税款总额
Download address of QT source code of each version
Yolov5 face learning notes
C language: callback function
Resolve importerror:lib*** so--cannot open shared object file: No such... (pycharm/clion reports an error but the shell does not)
C language: five custom types
计算两个时间相差的天数(支持跨月、跨年)
SpEL表达式 简单使用
Neo4j環境搭建
Neo4j Environment Building
Sort() sort function
Can the operation of the new BMW I3 meet the expectations of the famous products of the 3 series?
Collection of articles on virtualization and cloud computing
LeetCode 1143. 最长公共子序列
谨记! 写代码别太自信! 一定要写点关键日志info输出,不然问题都定位不到。
CAS无锁
LeetCode 5289. 公平分发饼干(DFS)
Agile development practice summary-4
Talking about acid of database