Javascript solution


  • 1
    Z
    /**
     * @param {number} x
     * @param {number} y
     * @return {number}
     */
    var hammingDistance = function(x, y) {
        let diff = x ^ y;
        let result = 0;
        
        // find number of ones in the XOR result
        while (diff > 0) {
            if (diff & 1 === 1) result += 1;
            diff >>= 1;
        }
        
        return result;
    };
    

  • 1
    D

    we need only one line

    /**
     * @param {number} x
     * @param {number} y
     * @return {number}
     */
    var hammingDistance = function(x, y) {
    	return new Number(x ^ y).toString(2).replace(/0/g, '').length;
    };
    

  • 0
    D
    This post is deleted!

Log in to reply
 

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