forked from tcandzq/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHammingDistance.py
More file actions
48 lines (37 loc) · 967 Bytes
/
HammingDistance.py
File metadata and controls
48 lines (37 loc) · 967 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/12/2 23:29
# @Author : tc
# @File : HammingDistance.py
"""
题号 461 汉明距离
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。
给出两个整数 x 和 y,计算它们之间的汉明距离。
注意:
0 ≤ x, y < 231.
示例:
输入: x = 1, y = 4
输出: 2
解释:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑
上面的箭头指出了对应二进制位不同的位置。
"""
class Solution:
# 正规解法
def hammingDistance(self, x: int, y: int) -> int:
res = x ^ y
count = 0
while res:
res = res & (res - 1)
count += 1
return count
# 非正规解法
def hammingDistance2(self, x: int, y: int) -> int:
return bin(x ^ y).count('1')
if __name__ == '__main__':
x = 1
y = 4
solution = Solution()
print(solution.hammingDistance(x,y))