LeetCode_201数字范围按位与

给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点)。

示例 1:

输入: [5,7]
输出: 4
示例 2:

输入: [0,1]
输出: 0

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/bitwise-and-of-numbers-range
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
    public int rangeBitwiseAnd(int m, int n) {
        int res = m;
        if(n==Integer.MAX_VALUE){
            n = n-1;//防止int类型溢出
        }
        for(int i=m;i<=n;i++){
            res = res&i;
            if(res==0){
                return 0;
            }
        }
        return res;
    }
}

你可能感兴趣的:(LeetCode题解)