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
[restabs alignment="osc-tabs-right" responsive="true" icon="true" text="More" seltabcolor="#fdfdfd" seltabheadcolor="#000" tabheadcolor="blue"]
[restab title="Java" active="active"]
public int rangeBitwiseAnd(int m, int n) {
while (n > m) {
n &= n & (n - 1);
}
return n & m;
}
[/restab]
[/restabs]
6.Test
[restabs alignment="osc-tabs-right" responsive="true" icon="true" text="More" seltabcolor="#fdfdfd" seltabheadcolor="#000" tabheadcolor="blue"]
[restab title="Java" active="active" ]
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));
}
}
[/restab]
[/restabs]



Comments | NOTHING