-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
45 lines (39 loc) · 684 Bytes
/
main.cpp
File metadata and controls
45 lines (39 loc) · 684 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<int> countBits(int n)
{
vector<int> answer(n + 1, 0);
for (int i = 0; i <= n; ++i)
answer[i] = answer[i >> 1] + (i & 1);
return answer;
}
};
int main()
{
int n = 5;
vector<int> output = Solution().countBits(n);
for (int el : output)
cout << el << " ";
cout << endl;
return 0;
}
// 0 0 0
// 1 1 1
// 2 10 1
// 3 11 2
// 4 100 1
// 5 101 2
// 6 110 2
// 7 111 3
// 8 1000 1
// 9 1001 2
// 10 1010 2
// 11 1011 3
// 12 1100 2
// 13 1101 3
// 14 1110 3
// 15 1111 4
// 16 10000 1