Total Accepted: 20178
Total Submissions: 36151
Difficulty: Medium
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.
Example:
For num = 5 you should return [0,1,1,2,1,2].
clclass Solution {
public:
vector<int> countBits(int num) {
vector<int>ans;
ans.push_back(0);
int res=0;
for(int i=1;i<=num;i++)
{
if((i&(i-1))==0)
{
res=i;
ans.push_back(1);
}
else
{
ans.push_back(ans[i-res]+1);
}
}
return ans;
}
};
本文介绍了一种方法,用于计算给定非负整数范围内每个整数的二进制表示中1的数量,并返回这些数量组成的数组。
509

被折叠的 条评论
为什么被折叠?



