Regex JS Solution


  • 0
    I
    var hammingWeight = function(n) {
        return (n.toString(2).match(/1/g) || []).length;
    };
    

    Convert Number to String of radix 2, search for matches on regular exp of 1 with global flag. If none, return empty array then find length of whole resuilt.


Log in to reply
 

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