Skip to content

Latest commit

 

History

History
29 lines (28 loc) · 501 Bytes

剑指 Offer II 003. 前 n 个数字二进制中 1 的个数.md

File metadata and controls

29 lines (28 loc) · 501 Bytes

https://leetcode-cn.com/problems/w3tCBm/

class Solution {
public:
    inline int count(int n)
    {
        int count=0;
        int base=1;
        while(base<=100000)
        {
            if(n&base)
            {
                count++;
            }
            base*=2;
        }
        return count;
    }
    vector<int> countBits(int n) {
        vector<int> res;
        for(int i=0;i<=n;++i)
        {
            res.push_back(count(i));
        }
        return res;
    }
};