Javascript O(n) One(ish) liner.


  • 0
    H

    This is technically O(n) with the caveat that it's probably a higher constant of n than other solutions with utilize bitwise operations.

    var hammingWeight = function(n) {
        return ((n >>> 0)
            .toString(2).split('').filter(num => num === '1').length);
    };
    

Log in to reply
 

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