Python Solution Using XOR


  • 0
    M
        def hammingDistance(self, x, y):
            """
            :type x: int
            :type y: int
            :rtype: int
            """
            ham_dist = 0
            while x or y:
                ham_dist += (x&1) ^ (y&1)
                x >>= 1
                y >>= 1
            return ham_dist
    

Log in to reply
 

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