A non-DP C++ solution


  • 0
    J
    class Solution {
    public:
        vector<int> countBits(int num) {
            vector<int> vec;vec.push_back(0);
            for(int i=1;i<=num;i++){
                int a=vec[i-1]+1;
                int k=(i|(i-1))-(i-1);
                while(!(k&1)){
                    a--;
                    k=k>>1;
                }
                vec.push_back(a);
            }
            return vec;
        }
    };

Log in to reply
 

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