forked from KnowledgeCenterYoutube/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path461_Hamming_Distance
More file actions
43 lines (35 loc) · 839 Bytes
/
Copy path461_Hamming_Distance
File metadata and controls
43 lines (35 loc) · 839 Bytes
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
Leetcode 461: Hamming Distance
Detailed video explanation: https://youtu.be/5UKMvO5bXPI
C++:
----
int hammingDistance(int x, int y) {
return bitset<32>(x^y).count();
}
int hammingDistance(int x, int y) {
int result = x^y;
int count = 0;
while(result > 0){
count += 1 & result;
result >>= 1;
}
return count;
}
Java:
-----
public int hammingDistance(int x, int y) {
int result = x^y;
int count = 0;
while(result>0){
count += result & 1;
result >>= 1;
}
return count;
}
Python3:
-------
def hammingDistance(self, x: int, y: int) -> int:
result, count = x^y, 0
while result>0:
count += result & 1
result >>= 1
return count