Bit operation. The key is to find common part from left side.
Solution:
# T:O(1) S:O(1) class Solution: # @param m, an integer # @param n, an integer # @return an integer def rangeBitwiseAnd(self, m, n): i, diff = 0, n-m while diff: diff >>= 1 i += 1 return n&m >> i << iRun Time: 196 ms
No comments:
Post a Comment