My solution in C++


  • 0
    Z

    This beats 99.16% of cpp submissions.

    // using ^ to compare the corresponding bits
    class Solution {
    public:
        int hammingDistance(int x, int y) {
            int num =0;
            while(x||y)
            {
                num += ((x & 0x01)^( y & 0x01));
                x>>=1;
                y>>=1;
            }
            return num;
            
        }
    };
    

    good fun!


Log in to reply
 

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