Javascript solution


  • 0
    E
    var hammingWeight = function(n) {
        var lst=n.toString(2).split("")
            ,l=lst.length
            ,ones=0;
        for (var i=0;i<l;++i) {
            if(lst[i]==="1") { ones++; }
        }
        return ones;
        // because it's a positive integer
    };

  • 0
    H

    regular expression can be used here :)


  • 0
    S

    I believe n is an unsigned integer, which means you need to convert the integer into its binary representation first.


Log in to reply
 

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