给你两个整数 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
方法一:位运算
题目可以转换为求数字的公共二进制前缀。
当
时间复杂度
class Solution:
def rangeBitwiseAnd(self, left: int, right: int) -> int:
while left < right:
right &= right - 1
return right
class Solution {
public int rangeBitwiseAnd(int left, int right) {
while (left < right) {
right &= (right - 1);
}
return right;
}
}
class Solution {
public:
int rangeBitwiseAnd(int left, int right) {
while (left < right) {
right &= (right - 1);
}
return right;
}
};
func rangeBitwiseAnd(left int, right int) int {
for left < right {
right &= (right - 1)
}
return right
}
public class Solution {
public int RangeBitwiseAnd(int left, int right) {
while (left < right) {
right &= (right - 1);
}
return right;
}
}
/**
* @param {number} left
* @param {number} right
* @return {number}
*/
var rangeBitwiseAnd = function (left, right) {
while (left < right) {
right &= right - 1;
}
return right;
};