461. Hamming Distance
- Description:
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x and y, calculate the Hamming distance.
class Solution(object):
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
self.x = x
self.y = y
self.z = self.x ^ self.y
return bin(self.z).count('1')
- Runtime: 33 ms
- Beat 98.14%