-
Notifications
You must be signed in to change notification settings - Fork 0
/
461
47 lines (37 loc) · 1.54 KB
/
461
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
461 the Hamming distance at: https://leetcode.com/problems/hamming-distance/description/
learnt a new function(built in): zip
zip([iterable, ...])
This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.
The returned list is truncated in length to the length of the shortest argument sequence.
When there are multiple arguments which are all of the same length, zip() is similar to map() with an initial argument of None.
With a single sequence argument, it returns a list of 1-tuples. With no arguments, it returns an empty list.
The left-to-right evaluation order of the iterables is guaranteed.
This makes possible an idiom for clustering a data series into n-length groups using zip(*[iter(s)]*n).
zip() in conjunction with the * operator can be used to unzip a list:
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> zipped
[(1, 4), (2, 5), (3, 6)]
>>> x2, y2 = zip(*zipped)
>>> x == list(x2) and y == list(y2)
True
class Solution(object):
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
diffs = 0
# print list(bin(x))[2:]
# print list(bin(y))[2:]
# exit()
int_x = int(bin(x)[2:])
int_y = int(bin(y)[2:])
res = str(int_x + int_y)
for i in range(len(res)):
if res[i] == '1':
diffs += 1
return diffs
this is a very simple question, no need to do looping