Skip to content

Commit 68151ce

Browse files
authored
Create 28 - Counting Bits.cpp
1 parent db1cdba commit 68151ce

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

28 - Counting Bits.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int count_one(int n){
4+
int res = 0;
5+
if(n == 0){
6+
return 0;
7+
}
8+
while(n > 0){
9+
if(n%2 == 1){
10+
res++;
11+
}
12+
n = n/2;
13+
}
14+
return res;
15+
}
16+
17+
18+
vector<int> countBits(int num) {
19+
vector<int> result;
20+
for(int i = 0; i<=num; i++){
21+
result.push_back(count_one(i));
22+
}
23+
return result;
24+
}
25+
};

0 commit comments

Comments
 (0)