也可以直接用 Integer.bitCount()
Then things left is to count how many
1
in above result. Integer.bitCount()
can help you do that.73.30 %
class Solution {
public int hammingDistance(int x, int y) {
int n = x ^ y;
int count = 0;
while (n != 0) {
count += (n & 1);
n >>>= 1;
}
return count;
}
}
No comments:
Post a Comment