Skip to content

Commit 0581a4f

Browse files
committed
Update counting-bits.cpp
1 parent f5d5a2d commit 0581a4f

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

C++/counting-bits.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
// Space: O(n)
33

44
class Solution {
5+
public:
6+
vector<int> countBits(int num) {
7+
vector<int> res{0};
8+
for (int i = 1; i <= num; ++i) {
9+
res.emplace_back(res[i >> 1] + (i & 1));
10+
}
11+
return res;
12+
}
13+
};
14+
15+
// Time: O(n)
16+
// Space: O(n)
17+
class Solution2 {
518
public:
619
vector<int> countBits(int num) {
720
vector<int> res{0};

0 commit comments

Comments
 (0)