Idea
直接暴力做法:计算从0到n,每一位数的二进制中1的个数,遍历其二进制的每一位即可得到1的个数
AC Code
class Solution {
public:vector<int> countBits(int n) {vector<int> ans;ans.emplace_back(0);for(int i = 1; i <= n; i++) {int count = 0;int j = i;while(j != 0) {if(j & 1) count++;j >>= 1;}ans.emplace_back(count);}return ans;}
};
