461. Hamming Distance
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 76% Topics: Bit Manipulation
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(1)
# Space: O(1)
class Solution(object):
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
distance = 0
z = x ^ y
while z:
distance += 1
z &= z - 1
return distance
def hammingDistance2(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
return bin(x ^ y).count('1')
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions