Skip to content

Commit 6f5a4f1

Browse files
committed
Solution: Counting Bits
1 parent 0876fb5 commit 6f5a4f1

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

counting-bits/flynn.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Time complexity: O(N)
3+
*
4+
* Space complexity: O(1)
5+
*/
6+
7+
class Solution {
8+
public:
9+
vector<int> countBits(int n) {
10+
vector<int> res;
11+
res.push_back(0);
12+
13+
int i = 1, j = 1;
14+
while (i <= n) {
15+
res.push_back(res[i - j] + 1);
16+
i++;
17+
if (i == j * 2) j *= 2;
18+
}
19+
20+
return res;
21+
}
22+
};

0 commit comments

Comments
 (0)