# Middle-题目59：201. Bitwise AND of Numbers Range

70人阅读 评论(0)

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.

public class Solution {
private static int[] pows = new int[]{1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824};
public static int rangeBitwiseAnd(int m, int n) {
if(m>pows[30])
return pows[30] + rangeBitwiseAnd(m-pows[30],n-pows[30]);
for(int i = 1;i < 31; i++) {
if(m<pows[i] && n>pows[i])
return 0;
else if(m>=pows[i] && n<pows[i+1])
return pows[i] + rangeBitwiseAnd(m-pows[i],n-pows[i]);
}
return m&n;
}
}

public class Solution {
public int rangeBitwiseAnd(int m, int n) {
int bit = 0;
while(m!=n) {
m>>=1;
n>>=1;
bit++;
}
return m<<bit;
}
}

Cmershen的碎碎念：

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：64396次
• 积分：3285
• 等级：
• 排名：第10717名
• 原创：270篇
• 转载：53篇
• 译文：0篇
• 评论：8条
文章分类
阅读排行
最新评论