-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path461-HammingDistance.py
More file actions
99 lines (89 loc) · 2.15 KB
/
461-HammingDistance.py
File metadata and controls
99 lines (89 loc) · 2.15 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Python3
class Solution:
def hammingDistance(self, x: int, y: int) -> int:
xList = list()
yList = list()
while x:
xList.append(x%2)
x = x // 2
while y:
yList.append(y%2)
y = y // 2
cnt = 0
while xList or yList:
if xList:
x = xList.pop(0)
else:
x = 0
if yList:
y = yList.pop(0)
else:
y = 0
if x != y:
cnt += 1
return cnt
class Solution:
def hammingDistance(self, x: int, y: int) -> int:
return bin(x ^ y).count('1')
# Java
class Solution {
public int hammingDistance(int x, int y) {
int cnt = 0;
if (x == y) {
return 0;
}
while (x!=0 || y!=0) {
if (x%2 != y%2) {
cnt++;
}
x = x / 2;
y = y / 2;
}
return cnt;
}
}
# C++
class Solution {
public:
int hammingDistance(int x, int y) {
x = x ^ y; // 2^4=6 010^100=110
y = 0; // 用y来计数
while (x) {
if (x % 2) {
y++;
}
x /= 2;
}
return y;
}
};
# 内置位计数功能:大多数编程语言都内置了计算二进制表达中 1 的数量的函数。
class Solution {
public:
int hammingDistance(int x, int y) {
return __builtin_popcount(x ^ y);
}
};
# Brian Kernighan 算法
# 通过"与"运算跳过两个 1 之间的 0,直接对 1 进行计数
# f(x) 表示 x 和 x-1 进行与运算所得的结果(即 f(x)=x & (x−1)),那么 f(x) 恰为 x 删去其二进制表示中最右侧的 1 的结果。
class Solution {
public:
int hammingDistance(int x, int y) {
int s = x ^ y, ret = 0;
while (s) {
s &= s - 1;
ret++;
}
return ret;
}
};
# Python3
class Solution:
def hammingDistance(self, x: int, y: int) -> int:
x = x ^ y
y = 0
while x:
x &= x - 1
y += 1
return y