Leetcode 461 Hamming Distance



分析:
或许这就是弱智题吧

思路:
既然要找位不同的地方,自然就会想到异或操作,异或的结果二进制中1的个数便是答案

1
2
3
4
5
6
7
8
9
10
11
12
class Solution(object):
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
return bin(x^y).count('1')

149 / 149 test cases passed.
difficulty: easy
Runtime: 41 ms