-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHamming Distance.py
More file actions
52 lines (42 loc) · 1.08 KB
/
Copy pathHamming Distance.py
File metadata and controls
52 lines (42 loc) · 1.08 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
'''
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.
Note:
0 ≤ x, y < 231.
Example:
Input: x = 1, y = 4
Output: 2
Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑
The above arrows point to positions where the corresponding bits are different.
'''
#Solution 1: Comparing each bit
class Solution:
def hammingDistance(self, x: int, y: int) -> int:
xx=bin(x).replace("0b", "")
yy=bin(y).replace("0b", "")
lx=len(xx)
ly=len(yy)
l=max(lx,ly)
if(lx>ly):
s='0'*(lx-ly)
yy=s+yy
else:
s='0'*(ly-lx)
xx=s+xx
c=0
for i in range(l):
if(xx[i]!=yy[i]):
c+=1
return c
#Solution 2:
class Solution:
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
return bin(x^y).count('1')