https://leetcode.com/problems/counting-bits/description/
vector<int> countBits(int num) {
vector<int> res;
for(int i=0; i<=num; i++) {
int n = i;
int b = 0;
while(n) {
b += n&1;
n >>= 1;
}
res.push_back(b);
}
return res;
}
No comments:
Post a Comment