Preview:
class Solution {
public:
    vector<int> countBits(int n) {
        vector<int> v(n+1, 0);
        for(int i=1;i<=n;i++)
        {
            int p=i;
            int t=32;
            int ans=0;
            
            while(p!=0)
            {
                int rsbm = p&-p;
                p-=rsbm;
                ans++;
            }
            v[i]=ans;
        }
        return v;
    }
};
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter