-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
338-Counting-Bits.cpp
45 lines (41 loc) · 927 Bytes
/
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
class Solution {
public:
vector<int> countBits(int n) {
vector<int> dp(n+1);
if(n == 0) return {0};
if(n == 1) return {0,1};
if(n==2) return {0,1,1};
if(n==3) return {0,1,1,2};
int flag;
dp[0] = 0;
dp[1] = 1;
dp[2] = 1;
dp[3] = 2;
for(int i = 4; i<= n; i++){
float j = log2(i);
if(j - int(j) == 0){
dp[i] = 1;
flag = i;
}else{
dp[i] = 1 + dp[i-flag];
}
}
return dp;
}
};
// Best Solution
class Solution {
public:
vector<int> countBits(int n) {
vector<int> res(n+1,0);
if(n == 0) return res;
for(int i=1; i<=n; i++){
if(i%2 == 0){
res[i] = res[i/2];
}else{
res[i] = 1 + res[i/2];
}
}
return res;
}
};