4 lines C++ 3ms


  • 0

    n&(n-1), eliminate the rightmost 1.

    class Solution {
    public:
        int hammingDistance(int x, int y) {
            int n = x^y;
            int d = 0;
            while(n) n &= n-1, d++;
            return d;
        }
    };
    

Log in to reply
 

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