TypechoJoeTheme

IT技术分享

统计

[LeetCode 201] Bitwise AND of Numbers Range [Java] [Runtime : 9MS]

2017-08-31
/
0 评论
/
605 阅读
/
正在检测是否收录...
08/31

1. Description

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.

2. Runtime Distribution

3. Submission Details

4. Example

Input: 5, 7
Ouput: 4

5. Code

public int rangeBitwiseAnd(int m, int n) {
    while (n > m) {
        n &= n & (n - 1);
    }
    return n & m;
}

6.Test

public class LeetCode0201 {
    public int rangeBitwiseAnd(int m, int n) {
        while (n > m) {
            n &= n & (n - 1);
        }
        return n & m;
    }

    public static void main(String[] args) {
        LeetCode0201 leetcode = new LeetCode0201();
        System.out.println(leetcode.rangeBitwiseAnd(0, 2147483647));
    }
}
BIT
朗读
赞 · 0
版权属于:

IT技术分享

本文链接:

https://idunso.com/archives/769/(转载时请注明本文出处及文章链接)