其他分享
首页 > 其他分享> > LeetCode 0201 Bitwise AND of Numbers Range

LeetCode 0201 Bitwise AND of Numbers Range

作者:互联网

原题传送门

1. 题目描述

2. Solution 1

1、思路分析
The hardest part of this problem is to find the regular pattern.
For example, for number 26 to 30
Their binary form are:
11010
11011
11100  
11101  
11110

Because we are trying to find bitwise AND, so if any bit there are at least one 0 and one 1, it always 0. In this
case, it is 11000.
So we are go to cut all these bit that they are different. In this case we cut the right 3 bit.

2、代码实现

package Q0299.Q0201BitwiseANDofNumbersRange;

/*
    The hardest part of this problem is to find the regular pattern.
    For example, for number 26 to 30
    Their binary form are:
    11010
    11011
    11100  
    11101  
    11110

    Because we are trying to find bitwise AND, so if any bit there are at least one 0 and one 1, it always 0. In this
    case, it is 11000.
    So we are go to cut all these bit that they are different. In this case we cut the right 3 bit.
 */
public class Solution {

    public int rangeBitwiseAnd(int left, int right) {
        int i = 0;  // i means we have how many bits are 0 on the right
        while (left != right) {
            left >>= 1;
            right >>= 1;
            i++;
        }
        return left << i;
    }
}

3、复杂度分析
时间复杂度: O(n), n = right - left
空间复杂度: O(1)

3. Solution 2

1、思路分析
Solution 1的递归实现。

2、代码实现

package Q0299.Q0201BitwiseANDofNumbersRange;

// recursive
public class Solution2 {
    public int rangeBitwiseAnd(int left, int right) {
        return (right > left) ? (rangeBitwiseAnd(left >> 1, right >> 1) << 1) : left;
    }
}

3、复杂度分析
时间复杂度: O(n), n = right - left
空间复杂度: O(n)

4. Solution 3

1、思路分析
分析
取 m = 18, n = 30
m = 18 -> 10010
n = 30 -> 11110
沿高位往低位比较,发现在8位出现了差异,由前面的分析可以导出,我们的结果为 10000 -> 16

可以取 m -> 10010 与 10000 相与可以得到结果,即 10010 & 10000 = 10000->16

对于任意情况如何构造 10000?
1) m^n = 01100
2) Integer.highestOneBit(m ^ n) -> 01000
 // highestOneBit:Returns an int value with at most a single one-bit, in the position of the highest-order
 // ("leftmost") one-bit in the specified int value.
3) ~Integer.highestOneBit(m ^ n) -> 10111
4) ~Integer.highestOneBit(m ^ n) + 1 -> 10000

2、代码实现

package Q0299.Q0201BitwiseANDofNumbersRange;

/*
    分析
    取 m = 18, n = 30
    m = 18 -> 10010
    n = 30 -> 11110
    沿高位往低位比较,发现在8位出现了差异,由前面的分析可以导出,我们的结果为 10000 -> 16

    可以取 m -> 10010 与 10000 相与可以得到结果,即 10010 & 10000 = 10000->16

    对于任意情况如何构造 10000?
    1) m^n = 01100
    2) Integer.highestOneBit(m ^ n) -> 01000
     // highestOneBit:Returns an int value with at most a single one-bit, in the position of the highest-order
     // ("leftmost") one-bit in the specified int value.
    3) ~Integer.highestOneBit(m ^ n) -> 10111
    4) ~Integer.highestOneBit(m ^ n) + 1 -> 10000
 */
public class Solution3 {

    public int rangeBitwiseAnd(int m, int n) {
        if (m == n) return m;
        return m & ~Integer.highestOneBit(m ^ n) + 1;
    }
}

3、复杂度分析
时间复杂度: O(1)
空间复杂度: O(1)

5. Solution 4

1、思路分析
分析: 结合补码的知识,按位取反,再加1,其实相当于取了一个相反数
~Integer.highestOneBit(m ^ n) + 1 <=> -Integer.highestOneBit(m ^ n)

2、代码实现

package Q0299.Q0201BitwiseANDofNumbersRange;

/*
    分析: 结合补码的知识,`按位取反,再加1`,其实相当于取了一个相反数
    ~Integer.highestOneBit(m ^ n) + 1 <=> -Integer.highestOneBit(m ^ n)
 */
public class Solution4 {
    public int rangeBitwiseAnd(int m, int n) {
        if (m == n) return m;
        return m & -Integer.highestOneBit(m ^ n);
    }
}

3、复杂度分析
时间复杂度: O(1)
空间复杂度: O(1)

5. Solution 5

1、思路分析
分析,追highestOneBit的源码为:
public static int highestOneBit(int i) {
// HD, Figure 3-1
i |= (i >> 1); // 把最高位的1复制给旁边,如,0001 xxxx -> 0001 1xxx,此时有2个1了
i |= (i >> 2); // 0001 1xxx -> 0001 111x
i |= (i >> 4); // ...
i |= (i >> 8); // ...
i |= (i >> 16); // 最终, 0001 xxxx -> 0001 1111
return i - (i >>> 1); // 0001 1111 - 0000 1111 = 0001 0000
}

取 m = 18, n = 30
m = 18 -> 10010
n = 30 -> 11110
沿高位往低位比较,发现在8位出现了差异,由前面的分析可以导出,我们的结果为 10000 -> 16
可以取 m -> 10010 与 10000 相与可以得到结果,即 10010 & 10000 = 10000->16
借用highestOneBit源码的手段,改进对于任意情况如何构造 10000?
1) m^n = 01100
2) 01100 -> 01111
3) ~01111 -> 10000

2、代码实现

package Q0299.Q0201BitwiseANDofNumbersRange;

/*
    分析,追highestOneBit的源码为:
    public static int highestOneBit(int i) {
        // HD, Figure 3-1
        i |= (i >>  1);  // 把最高位的1复制给旁边,如,0001 xxxx -> 0001 1xxx,此时有2个1了
        i |= (i >>  2);  // 0001 1xxx -> 0001 111x
        i |= (i >>  4);  // ...
        i |= (i >>  8);  // ...
        i |= (i >> 16);  // 最终, 0001 xxxx -> 0001 1111
        return i - (i >>> 1); // 0001 1111 - 0000 1111 = 0001 0000
    }

    取 m = 18, n = 30
    m = 18 -> 10010
    n = 30 -> 11110
    沿高位往低位比较,发现在8位出现了差异,由前面的分析可以导出,我们的结果为 10000 -> 16
    可以取 m -> 10010 与 10000 相与可以得到结果,即 10010 & 10000 = 10000->16
    借用highestOneBit源码的手段,改进对于任意情况如何构造 10000?
    1) m^n = 01100
    2) 01100 -> 01111
    3) ~01111 -> 10000
 */
public class Solution5 {
    public int rangeBitwiseAnd(int m, int n) {
        if (m == n) return m;
        int i = m ^ n;
        i |= (i >> 1);
        i |= (i >> 2);
        i |= (i >> 4);
        i |= (i >> 8);
        i |= (i >> 16);
        return m & ~i;
    }
}

3、复杂度分析
时间复杂度: O(1)
空间复杂度: O(1)

标签:10000,0001,int,复杂度,public,Range,Numbers,highestOneBit,LeetCode
来源: https://www.cnblogs.com/junstat/p/16336400.html