[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 creating all test cases.

又是一道考察位操作Bit Operation的题,相似的题目在LeetCode中还真不少,比如Repeated DNA Sequences 求重复的DNA序列Single Number 单独的数字,  Single Number II 单独的数字之二 ,Grey Code 格雷码,和Reverse Bits 翻转位 等等,那么这道题其实并不难,我们先从题目中给的例子来分析,[5, 7]里共有三个数字,分别写出它们的二进制为:

101  110  111

相与后的结果为100,仔细观察我们可以得出,最后的数是该数字范围内所有的数的左边共同1的部分,如果上面那个例子不太明显,我们再来看一个范围[26, 30],它们的二进制如下:

11010  11011  11100  11101  11110

发现了规律后,我们只要写代码找到左边公共1的部分即可,我们可以从建立一个32位都是1的mask,然后每次向左移一位,比较m和n是否相同,不同再继续左移一位,直至相同,然后把m和mask相与就是最终结果,代码如下:

解法一

class Solution {
public:
    int rangeBitwiseAnd(int m, int n) {
        int d = INT_MAX;
        while ((m & d) != (n & d)) {
            d <<= 1;
        }
        return m & d;
    }
};

此题还有另一种解法,不需要用mask,直接平移m和n,每次向右移一位,直到m和n相等,记录下所有平移的次数i,然后再把m左移i位即为最终结果,代码如下:

解法二

class Solution {
public:
    int rangeBitwiseAnd(int m, int n) {
        int i = 0;
        while (m != n) {
            m >>= 1;
            n >>= 1;
            ++i;
        }
        return (m << i);
    }
};
时间: 2024-09-30 07:35:34

[LeetCode] Bitwise AND of Numbers Range 数字范围位相与的相关文章

Leetcode Bitwise AND of Numbers Range

题目地址:https://leetcode.com/problems/bitwise-and-of-numbers-range/ 题目解析:参考https://leetcode.com/problems/number-of-1-bits/,我们知道n&n-1为n和n-1两者左边相同的部分,那么容易得到n-1&n-2为n-2与n-1左边相同部分,依次类推,m&m+1....&n为m与n左边相同部分. 题目解答: public class Solution { public i

[leetcode] Bitwise AND of Numbers Range(位运算)

不得不说位运算的应用都是很巧妙的. 这道题求一个连续区间整数的与运算的值,突破点在于连续的区间这个特点.我们可以先找几个数看一下规律, 2 3 4 5的二进制分别是 10 ,11,100,101,可以发现若m==n,则m为答案:当m!=n时,因为连续的两个数的二进制 的最后一位肯定不一样,与的值一定是0,前面相同的部分(1&1=1,0&0=0)保持. 所以我们每次先判断,不同的话就右移一位,比较前面的,直到相同的时候结束,最后左移  移动的位数. 不必担心左移的时候有1会导致溢出,从而失败

【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

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_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

【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. 思

[Swift]LeetCode201. 数字范围按位与 | 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. Example 1: Input: [5,7] Output: 4 Example 2: Input: [0,1] Output: 0 给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字

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