-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounting-bits.cpp
More file actions
52 lines (44 loc) · 796 Bytes
/
counting-bits.cpp
File metadata and controls
52 lines (44 loc) · 796 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
46
47
48
49
50
51
52
/*
* Copyright (c) 2019 Christopher Friedt
*
* SPDX-License-Identifier: MIT
*/
#include <array>
#include <vector>
using namespace std;
class Solution {
public:
// https://leetcode.com/problems/counting-bits/
vector<int> countBits(int num) {
vector<int> r(num + 1, 0);
for (int i = 0; i <= num; i++) {
r[i] = ones(i);
}
return r;
}
protected:
static const array<int, 16> lut;
static int ones(uint8_t x) { return lut[x & 0xf] + lut[(x >> 4) & 0xf]; }
static int ones(int x) {
uint8_t *px = (uint8_t *)&x;
return ones(px[0]) + ones(px[1]) + ones(px[2]) + ones(px[3]);
}
};
const array<int, 16> Solution::lut = array<int, 16>{{
0,
1,
1,
2,
1,
2,
2,
3,
1,
2,
2,
3,
2,
3,
3,
4,
}};