C++, DP


  • 0
    H

    class Solution
    {
    public:
    vector <int> countBits(int n)
    {
    vector<int> BitsNum(n + 1, 0);
    BitsNum[0] = 0;
    BitsNum[1] = 1;
    if( n == 1) return BitsNum;
    int i = 1;
    int Nf = 2;
    while( i <= n)
    {

            for(  i = Nf; i < 2*Nf && i <= n; i++)
            {
                BitsNum[i] = BitsNum[i - Nf] + 1; 
            }
            Nf += Nf; 
        }
        return BitsNum; 
    }
    

    };


Log in to reply
 

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