461. Hamming Distance

461. Hamming Distance_第1张图片

Java

public class Solution {
    public int hammingDistance(int x, int y) {
        int c=x^y;
        int i;
        for(i=0;c>0;i++)
        {
            c&=(c-1);
        }
        return i;
    }
}

Java,很简练,用到了库函数

public class Solution {
    public int hammingDistance(int x, int y) {
        return Integer.bitCount(x ^ y);
    }
}

你可能感兴趣的:(461. Hamming Distance)