1. 程式人生 > >[Leetcode,python] Hamming Distance 漢明距離

[Leetcode,python] 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, calculate the Hamming distance.

解決方案:

class Solution(object):
    def hammingDistance(self, x, y):
        """
        :type x: int
        :type y: int
        :rtype: int
        """
return bin(x^y).count('1')