Python Simple to Understand Beats 100 %


  • 0
    A

    XOR would give the differences. Then just count the number of bits which are set

        def hammingDistance(self, x, y):
            """
            :type x: int
            :type y: int
            :rtype: int
            """
            xor_val = x ^ y
            count = 0
            while xor_val:
                xor_val = xor_val & (xor_val - 1)
                count += 1
            return count
    

Log in to reply
 

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