Hamming Distance LeetCode solution in Python

1
class Solution:
2
def hammingDistance(self, x: int, y: int) -> int:
3
# First, using XOR Bitwise Operator, we take all distinct set bits.
4
z = x ^ y
5
# We inicialize our answer with zero.
6
ans = 0
7
# Iterate while our z is not zero.
8
while z:
9
# Every iteration we add one to our answer.
10
ans += 1
11
# Using the expression z & (z - 1), we erase the lowest set bit in z.
12
z &= z - 1
13
return ans

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0