Javascript solution

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

  • 0

    regular expression can be used here :)

  • 0

    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.