Friday, April 1, 2016

LeetCode Q201: Bitwise AND of Numbers of Range

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.

Solution 1:
Need to tell whether each bit contains a zero? Basically, if m - n > 2^i, where i represents for i-th bit. then i-th bit must be zero in result.


Solution 2:
Consider the bits from low to high. if n>m, the lowest bit will be 0, then we could transfer the problem to sub-problem.



Round 2 solution:

Rnd3 sol:

No comments:

Post a Comment