82ms JavaScript Solution:intuitive


  • 0

    This solution isn't the fastest one,but it's the most intuitive one I think. HaHa

    /**
     * @param {number} x
     * @param {number} y
     * @return {number}
     */
    var hammingDistance = function(x, y) {
        let sum = 0;
        while(x||y){
            if(x%2!=y%2){
                sum++;
            }
            x = (x - x % 2) / 2;
            y = (y - y % 2) / 2;
        }
        return sum;
    };
    

Log in to reply
 

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