Python solution


  • 0
    T
    class Solution(object):
        def hammingDistance(self, x, y):
            # 1. Get the different bits using XOR
            xor = x ^ y
            
            # 2. Count the number of 1s
            count = 0
            while xor:
                xor = xor & (xor - 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.