-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion_2.py
More file actions
65 lines (52 loc) · 1.39 KB
/
question_2.py
File metadata and controls
65 lines (52 loc) · 1.39 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/6/27 23:11
# @Author : cancan
# @File : question_2.py
# @Function : 汉明距离
"""
Question:
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。
给出两个整数 x 和 y,计算它们之间的汉明距离。
Note:
0 ≤ x, y < 231.
Example:
输入: x = 1, y = 4
输出: 2
解释:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑
上面的箭头指出了对应二进制位不同的位置。
"""
class Solution(object):
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
list_x = list(reversed(bin(x)[2:]))
list_y = list(reversed(bin(y)[2:]))
len_x = len(list_x)
len_y = len(list_y)
if len_x > len_y:
count = self.get_count(len_x, list_x, list_y)
else:
count = self.get_count(len_y, list_y, list_x)
return count
def get_count(self, list_len, list_x, list_y):
count = 0
t = 0
for i, v in enumerate(list_y):
if list_x[i] != v:
count += 1
t = i
if t + 1 < list_len:
for ii in list_x[t + 1:]:
if ii == '1':
count += 1
return count
if __name__ == "__main__":
s = Solution()
print(s.hammingDistance(0, 10))