diff options
Diffstat (limited to 'hamming_distance.py')
-rw-r--r-- | hamming_distance.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/hamming_distance.py b/hamming_distance.py new file mode 100644 index 0000000..715b35a --- /dev/null +++ b/hamming_distance.py @@ -0,0 +1,27 @@ +# 461. Hamming Distance + +""" +the hamming distance between two integers is the number of positions at which +the corresponding bits are different. given two integers 'x' and 'y', return +the hamming distance between them. +""" + + +class Solution(object): + def hammingDistance(self, x, y): + """ + :type x: int + :type y: int + :rtype: int + """ + ans, n = 0, x ^ y + while n: + ans += 1 + n &= n - 1 + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.hammingDistance(1, 4)) + print(obj.hammingDistance(3, 1)) |