forked from sachuverma/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounting Bits.cpp
47 lines (41 loc) · 866 Bytes
/
Counting Bits.cpp
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
46
47
/*
Counting Bits
=============
Given an integer num, return an array of the number of 1's in the binary representation of every number in the range [0, num].
Example 1:
Input: num = 2
Output: [0,1,1]
Explanation:
0 --> 0
1 --> 1
2 --> 10
Example 2:
Input: num = 5
Output: [0,1,1,2,1,2]
Explanation:
0 --> 0
1 --> 1
2 --> 10
3 --> 11
4 --> 100
5 --> 101
Constraints:
0 <= num <= 105
Follow up:
It is very easy to come up with a solution with run time O(32n). Can you do it in linear time O(n) and possibly in a single pass?
Could you solve it in O(n) space complexity?
Can you do it without using any built-in function (i.e., like __builtin_popcount in C++)?
*/
class Solution
{
public:
vector<int> countBits(int num)
{
vector<int> ans(num + 1, 0);
for (int i = 1; i <= num; ++i)
{
ans[i] = ans[i / 2] + i % 2;
}
return ans;
}
};