Bitwise AND of Numbers Range——LeetCode

class Solution {

public:

/*

mask的32位全为1,mask<<i是将mask的后i位置0,此时m或n和mask进行与操作,实际上就是将m或n的(副本)后i位置0。这时再进行异或操作,其实就是比较m和n的前32-i位是否相等,若相等,则停止循环,因为这相等的32-i位相与不会改变各个bit。又因m~n是连续变化的,后i位的各个bit为0和为1的情况都会出现,也就是说,m~n这些数相与后,后i位必定为0.综上,m和n后i位相与为0,前32-i位相同,从而m或n & mask<<i即是所求的值。

*/

int rangeBitwiseAnd(int m, int n) {

int i = 0, mask= ~0;

while ((m & mask<<i) ^ (n & mask<<i))

{

++i;

}

return m & mask<<i ;

}

};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-03 20:59:34

Bitwise AND of Numbers Range——LeetCode的相关文章

201. Bitwise AND of Numbers Range Leetcode Python

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. Credits: Special thanks to @amrsaqr for adding this problem and creati

【LeetCode】201. Bitwise AND of Numbers Range

Bitwise AND of Numbers Range  Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. Credits:Special thanks to @amrsaqr for a

leetcode_201题——Bitwise AND of Numbers Range(位操作)

Bitwise AND of Numbers Range Total Accepted: 9698 Total Submissions: 35771My Submissions Question Solution Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given th

leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number

一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000000001011, so t

【Leetcode】Bitwise AND of Numbers Range

题目链接:https://leetcode.com/problems/bitwise-and-of-numbers-range/ 题目: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. 思

[LeetCode] Bitwise AND of Numbers Range 数字范围位相与

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. Credits:Special thanks to @amrsaqr for adding this problem and creatin

Baozi Leetcode solution 201: Bitwise AND of Numbers Range

Problem Statement Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. Example 1: Input: [5,7] Output: 4 Example 2: Input: [0,1] Output: 0 Problem link Video Tutorial You can find t

[LeetCode#201] Bitwise AND of Numbers Range

Problem: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. Analysis: The idea behind this problem is not hard, you could

【leetcode】Bitwise AND of Numbers Range(middle)

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. 思路: 先找前面二进制相同的位,后面不相同的位相与一定为0. 比如: 1111001 1110011 从0011 - 1001 中间一定会经