My js solution ,260ms


  • 0
    P

    in js ,bitwise operations are slowly.so it not recommanded.

    var countBits = function(num) {
        var r = [0];
        var range = 1;
        for(var i=1;i<=num;i++){      
            if(i === 2*range){
                range *= 2;
            }
            if(i >= range && i < 2* range){            
                r.push(r[i-range]+1);
            }
        }
        return r;
    };

Log in to reply
 

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