counting-bits 1.0.0
Counting Bits
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <bits/stdc++.h>
2
3using namespace std;
4
6{
7public:
8 vector<int> countBits(int n)
9 {
10 vector<int> answer(n + 1, 0);
11
12 for (int i = 0; i <= n; ++i)
13 answer[i] = answer[i >> 1] + (i & 1);
14
15 return answer;
16 }
17};
18
19int main()
20{
21 int n = 5;
22 vector<int> output = Solution().countBits(n);
23 for (int el : output)
24 cout << el << " ";
25 cout << endl;
26 return 0;
27}
28
29// 0 0 0
30// 1 1 1
31// 2 10 1
32// 3 11 2
33// 4 100 1
34// 5 101 2
35// 6 110 2
36// 7 111 3
37// 8 1000 1
38// 9 1001 2
39// 10 1010 2
40// 11 1011 3
41// 12 1100 2
42// 13 1101 3
43// 14 1110 3
44// 15 1111 4
45// 16 10000 1
vector< int > countBits(int n)
Definition main.cpp:8
int main()
Definition main.cpp:19