ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 0201 Bitwise AND of Numbers Range

2022-06-02 08:33:38  阅读:200  来源: 互联网

标签:10000 0001 int 复杂度 public Range Numbers highestOneBit LeetCode


原题传送门

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有