Solution in javascrip without bitwise operations


  • 0
    R

    I hava used seperated these numbers by groups. In each group, numbers observe the same rules. For the number of each group- group_number will increase by the power of 2.

    /**
         * @param {number} num
         * @return {number[]}
         */
        var countBits = function(num) {
        
        //an array for stoking the number of each number
        var number = new Array();
            
        var i = 1;
        var group = 0;
        var group_number = 1;
    
        number.push(0);
    
        while(i<=num){
            
            //operations in each group
            if(i<group_number*2){
                number.push(number[i-group_number]+1);
            }
            else{
                group++;
                group_number=group_number*2;
                continue;
            }
            i++;
        }
        return number;
    };

Log in to reply
 

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