461.Hamming Distance

Solution 1

class Solution {
    public int hammingDistance(int x, int y) {
        int ans = 0;
        while(x>0 || y>0){
            ans += ((1 & x) ^ (1 & y));
            x >>= 1;
            y >>= 1;
        }
        return ans;
    }
}
class Solution {
    public int hammingDistance(int x, int y) {
        int ans = 0;
        int tmp = x ^ y;
        while(tmp > 0){
            ans += tmp & 1;
            tmp >>= 1;
        }
        return ans;
    }
}

results matching ""

    No results matching ""