-
Notifications
You must be signed in to change notification settings - Fork 19
/
answer.py
36 lines (30 loc) · 915 Bytes
/
answer.py
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
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
class Solution:
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
return bin(x ^ y).count('1')
#-------------------------------------------------------------------------------
class Solution(object):
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
if x >= y:
x = bin(x)[2:]
y = bin(y)[2:].zfill(len(x))
else:
y = bin(y)[2:]
x = bin(x)[2:].zfill(len(y))
count = 0
for i in range(len(x)):
if x[i] != y[i]:
count += 1
return count
#-------------------------------------------------------------------------------