201:数字范围按位与

This commit is contained in:
huangge1199 2021-08-20 15:34:44 +08:00
parent 85302ed868
commit 452a51ed10

View File

@ -0,0 +1,60 @@
//给你两个整数 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 <= 2³¹ - 1
//
// Related Topics 位运算 👍 309 👎 0
package leetcode.editor.cn;
//201:数字范围按位与
class BitwiseAndOfNumbersRange {
public static void main(String[] args) {
//测试代码
Solution solution = new BitwiseAndOfNumbersRange().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int rangeBitwiseAnd(int left, int right) {
int mov = 0;
while (left < right) {
left >>= 1;
right >>= 1;
mov++;
}
return left << mov;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}