C Language, Easy understand! O(1) space


  • 0
    G
    int hammingDistance(int x, int y) {
        if (x < 0 || y < 0)
            return -1;
        x = x ^ y;
        int n = 0;
        while (0 < x) {
            if (1 == (x & 1))
                n++;
            x = x >> 1;
        }
    
        return n;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.