my javascript solution using dynamic programming


  • 0
    C

    my javascript solution using dynamic programming

    var countBits = function(num) {
        if(num===0) return [0]
        var arr = [0,1],lastPowOf2 = 1;
        for(var i=2;i<=num;i++){
        	if(i===lastPowOf2*2){
        		lastPowOf2 = i;
        		arr[i] = 1;
        	}
        	else{
        		var gap = i-lastPowOf2;
        		arr[i] = arr[lastPowOf2] + arr[gap]
        	}
        }
        return arr;
    };
    

Log in to reply
 

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