C++ O(n) Solution


  • 2
    P
    class Solution {
    public:
        vector<int> countBits(int num) {
            vector<int> out;
            out.push_back(0);
            if(num>0) out.push_back(1);
            for(int i=2; i<=num; i++){
                int n1 = i/2;
                int n2 = i%2;
                out.push_back(out[n1] + out[n2]);
            }
            return out;
        }
    };

Log in to reply
 

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