标签:
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.
Credits:
Special thanks to @amrsaqr for adding this problem and creating all test cases.
class Solution { public: int rangeBitwiseAnd(int m, int n) { int ret = m; int a = m, b = n; int lena = 1, lenb = 1; if(m == n) return m; while(a >>= 1) lena++; while(b >>= 1) lenb++; if(lena == lenb) { for(int i = m + 1; i <= n; i++) { ret &= i; if(i == INT_MAX) break; } } else ret = 0; return ret; } };
[leetcode] Bitwise AND of Numbers Range
标签:
原文地址:http://www.cnblogs.com/lxd2502/p/4479970.html