[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 easily think out the solution. But for the implementation, you may trap yourself into some sterotypes.
Idea:
Since for bitwise ‘AND‘ operation, as long as there is a ‘0‘ appears, the digit should become 0.

Principle in digit change in range: (work for numerial system, binary system and other system too)
Start:  1011111100111010101010
End:    1011111110001010010101

To reach 10001010010101 from 00111010101010. We must start to add
00000000000001 onto 00111010101010 => 00111010101011
...
We can we reach the ‘1‘ in high index, all digits behind after it must change once. That‘s to say, all the digits after 1 (include 1) should become 0.
answer: 1011111110000000000000

Algorithm:
Step 1: Find the first left bit ‘start‘ differ from ‘end‘.
Start:  10111111[0]0111010101010
End:    10111111[1]0001010010101

Step 2: Make all bits after the first different bits (include first different bit) into 0. The number is the answer we wish to get.
    10111111[0]0111010101010
=>  10111111000000000000

Great implementation:
Move bit to reach the answer!!!! See the solution!

Solution:

public class Solution {
    public int rangeBitwiseAnd(int m, int n) {
        if (m < 0 || n < 0)
            throw new IllegalArgumentException("the passed in arguements is not tin the valid range!");
        int p = 0;
        while (m != n) {
            m = m >> 1;
            n = n >> 1;
            p++;
        }
        return m << p;
    }
}
时间: 2024-10-12 08:13:33

[LeetCode#201] Bitwise AND of Numbers Range的相关文章

leetcode 201. Bitwise AND of Numbers Range(位运算,dp)

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. 题解:如果m==n,那么答案就是m. 如果m<n,那么二进制最右边一位在最后的结果中肯定是0,那么就可以转化成子问题: rangeBi

Java for LeetCode 201 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. 解题思路: 本题有很多思路,最简单的方法: result就是m和n二进制前面相同的部分!!! JAVA实现如下: public int ra

【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

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

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

Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 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(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 中间一定会经

[leedcode 201] 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. public class Solution { public int rangeBitwiseAnd(int m, int n) { /*①