leetcode 461. Hamming Distance

来源:互联网 发布:竞彩足球奖金优化 编辑:程序博客网 时间:2024/05/22 11:49

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')

bin函数:将int转换成二进制bit的字符串,前面会有’0b’
bin(5)=’0b101’

0 0
原创粉丝点击