C++ 92 ms solution with dp


  • 0
    G
    class Solution {
    public:
        vector<int> result;
        vector<int> countBits(int num) {
            
            result.push_back(0);
            
            if(num < 1)
                return result;
                
            int i = 1,index = 0;
            while(i <= num) {
                if(!(i & i-1))
                {
                    index = 0;
                }
                else
                {
                    index++;
                }
                i++;
                result.push_back(result[index]+1);
            }
            return result;
        }
    };

Log in to reply
 

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