201. Bitwise AND of Numbers Range
Aug 12, 2016
https://leetcode.com/problems/bitwise-and-of-numbers-range/
Brutal
Use loop for every number between m and n, &
together. O(n - m)
Revision 1
To bitwise and
two number n1
and n2
is to take the common set bits. eg: 1100 & 1011 = 1000
.
So, we only need to find the common 1s
for all of the number from m
to n
:
|
|
Revision 2
Like the number-of-1-bits
The n & (n - 1)
will remove the right most set bit
to 0
, this is like moving one step
closer to m
. Or, in other word, making it more “alike” to m
.
|
|