Skip to content

Commit 5cea37c

Browse files
committed
Time: 6 ms (65.80%), Space: 7.8 MB (88.58%) - LeetHub
1 parent f56c83c commit 5cea37c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int countSetBits(int n) {
4+
int count = 0;
5+
while(n > 0){
6+
n &= n-1;
7+
count++;
8+
}
9+
return count;
10+
}
11+
12+
vector<int> countBits(int n) {
13+
vector<int> ans(n+1, 0);
14+
15+
for(int i=1;i<=n;i++){
16+
ans[i] = countSetBits(i);
17+
}
18+
return ans;
19+
}
20+
};

0 commit comments

Comments
 (0)