方法1:
class Solution {
public int hammingDistance(int x, int y) {
int xor = x ^ y;
int ans = 0;
while (xor != 0) {
xor -= xor & (-xor);
ans++;
}
return ans;
}
}
方法2:
class Solution {
public int hammingDistance(int x, int y) {
return Integer.bitCount(x ^ y);
}
}