Lintcode 835. Hamming Distance (Easy) (Python)
835. Hamming Distance
Description:
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.
Example
Input: x = 1, y = 4
Output: 2
Code:
Description:
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.
Example
Input: x = 1, y = 4
Output: 2
Code:
class Solution:
"""
@param x: an integer
@param y: an integer
@return: return an integer, denote the Hamming Distance between two integers
"""
def hammingDistance(self, x, y):
# write your code here
return bin(x^y).count('1')