题目:
汉明距离指的是两个二进制数对应位置内容不同的位数。
解题思路:
使用python中的^(按位异或)操作,异或操作就是内容相同为0,否则为1。再对异或的结果统计其二进制数中1的个数即可。
代码(Python):
class Solution(object):
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
z = x^y
count = 0
while(z):
count = count + z%2
z = z/2
return count