class Solution {
public:
vector<int> countBits(int num) {
vector<int> V;
V.push_back(0);
for(int ii = 1; ii <= num; ii++) {
int cnt = 0;
for(int bit = 1; bit <= ii; bit <<= 1) if(ii & bit) cnt++;
V.push_back(cnt);
}
return V;
}
};
【leetcode 位运算 C++】338. Counting Bits
最新推荐文章于 2024-01-31 11:12:38 发布