An easy understand solution in c++


  • 0
    C
    vector<int> countBits(int num) {
        vector<int> sums(num+1, 0);
        if (num == 0)
    	{
    		return sums;
    	}
    	int i,j = 1;
    	for (i = 1; i <= num; i++)
    	{
    		if (!((i - 1) & i))
    		{
    			j = 1;
    			sums[i] = 1;
    			continue;
    		}
    		sums[i] = sums[j++] + 1;
    	}
        return sums;
    }

Log in to reply
 

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