forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_461.java
33 lines (30 loc) · 789 Bytes
/
_461.java
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
package com.fishercoder.solutions;
public class _461 {
public static class Solution1 {
public int hammingDistance(int x, int y) {
int n = x ^ y;
int count = 0;
while (n != 0) {
count++;
n &= (n - 1);
}
return count;
}
}
public static class Solution2 {
public int hammingDistance(int x, int y) {
int ans = 0;
for (int i = 0; i < 32; i++) {
ans += (x & 1) ^ (y & 1);
x >>= 1;
y >>= 1;
}
return ans;
}
}
public static class Solution3 {
public int hammingDistance(int x, int y) {
return Integer.bitCount(x ^ y);
}
}
}