C++ 2-line solution without built-in function


  • 0
    C

    The main idea is to calculate the bitcount of x & y.

    class Solution {
    public:
        int hammingDistance(int x, int y) {
            for (y ^= x, x = 0; y; y &= ~(y & -y), x++);
            return x;
        }
    };
    

Log in to reply
 

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