Skip to content

Latest commit

 

History

History
70 lines (63 loc) · 1.24 KB

File metadata and controls

70 lines (63 loc) · 1.24 KB

201. 数字范围按位与

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

示例 1:

输入: [5,7]
输出: 4

示例 2:

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

题解 (Rust)

1. 题解1

impl Solution {
    pub fn range_bitwise_and(m: i32, n: i32) -> i32 {
        let mut result = n;
        for i in m..n {
            result &= i;
        }
        result
    }
}

2. 题解2

impl Solution {
    pub fn range_bitwise_and(m: i32, n: i32) -> i32 {
        let mut n = n;
        while n > m {
            n &= n - 1;
        }
        n
    }
}

3. 题解3

impl Solution {
    pub fn range_bitwise_and(m: i32, n: i32) -> i32 {
        if n > m {
            Self::range_bitwise_and(m >> 1, n >> 1) << 1
        } else {
            m
        }
    }
}

4. 题解4

impl Solution {
    pub fn range_bitwise_and(m: i32, n: i32) -> i32 {
        let mut result = 0;
        let mut mask = 1 << 30;
        while mask != 0 && m & mask == n & mask {
            result |= m & mask;
            mask >>= 1;
        }
        result
    }
}