-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path338-counting-bits.cpp
87 lines (71 loc) · 2.51 KB
/
338-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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// 338. Counting Bits
//
// Given a non negative integer number num.
// For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and
// return them as an array.
//
// Example:
// For num = 5 you should return [0,1,1,2,1,2].
//
// Follow up:
// It is very easy to come up with a solution with run time O(n*sizeof(integer)).
// But can you do it in linear time O(n) /possibly in a single pass?
// Space complexity should be O(n).
//
// Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
//
// Hints:
// * You should make use of what you have produced already.
// * Divide the numbers in ranges like [2-3], [4-7], [8-15] and so on. And try to generate new range from previous.
// * Or does the odd/even status of the number help you in calculating the number of 1s?
//
// Tags: Dynamic Programming, Bit Manipulation
//
// https://leetcode.com/problems/counting-bits/
#include <iostream>
#include <gtest/gtest.h>
using namespace std;
class Solution {
public:
vector<int> countBits(int num) {
vector<int>result(num+1, 0);
for (int i = 0; i <= num / 2; ++i) {
result[2 * i] = result[i];
result[2 * i + 1] = result[i] + 1;
}
return result;
}
};
bool vector_equal(vector<int>&a, vector<int>&b){
if(a.size() != b.size()) return false;
for (int i = 0; i < a.size(); ++i) {
if(a[i] != b[i]) return false;
}
return true;
}
TEST(leetcode_338_counting_bits, Basic)
{
Solution *solution = new Solution();
vector<int> result = solution->countBits(5);
vector<int> expected{0, 1, 1, 2, 1, 2};
EXPECT_TRUE(vector_equal(expected, result));
result = solution->countBits(0);
expected = {0};
EXPECT_TRUE(vector_equal(expected, result));
result = solution->countBits(1);
expected = {0, 1};
EXPECT_TRUE(vector_equal(expected, result));
result = solution->countBits(6);
expected = {0, 1, 1, 2, 1, 2, 2};
EXPECT_TRUE(vector_equal(expected, result));
result = solution->countBits(10);
expected = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2};
EXPECT_TRUE(vector_equal(expected, result));
result = solution->countBits(20);
expected = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2};
EXPECT_TRUE(vector_equal(expected, result));
}
int main(int argc, char *argv[]) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}