Python solution share based on bit manipulation


  • 0
    Z
    class Solution(object):
        def rangeBitwiseAnd(self, m, n):
            """
            :type m: int
            :type n: int
            :rtype: int
            """
            t = m ^ n
            t = t | t >> 1
            t = t | t >> 2
            t = t | t >> 4
            t = t | t >> 8
            t = t | t >> 16
            return n & (2147483647-t)

  • 0

    Could you explain your reasoning?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.