-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathcount-binary-palindromic-numbers.cpp
More file actions
55 lines (50 loc) · 1.42 KB
/
count-binary-palindromic-numbers.cpp
File metadata and controls
55 lines (50 loc) · 1.42 KB
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
// Time: O(logn)
// Space: O(1)
// bitmasks, combinatorics
class Solution {
public:
int countBinaryPalindromes(long long n) {
const auto& length = [&](int64_t n) {
int result = 0;
for (; n; n >>= 1) {
++result;
}
return result;
};
const auto& reverse = [](int64_t n, int l) {
int64_t result = 0;
for (int i = 0; i < l; ++i) {
if (n & (1 << i)) {
result |= 1 << ((l - 1) - i);
}
}
return result;
};
const auto& l = length(n) / 2;
const auto& p = ((n >> l) << l) | reverse(n >> (length(n) - l), l);
return ((1 << l) - 1) + (n >> l) + (p <= n ? 1 : 0);
}
};
// Time: O(logn)
// Space: O(logn)
// bitmasks, combinatorics
class Solution2 {
public:
int countBinaryPalindromes(long long n) {
const auto& to_binary = [&](int64_t n) {
string result;
for (; n; n >>= 1) {
result.push_back(n & 1);
}
reverse(begin(result), end(result));
return result;
};
const auto& s = to_binary(n);
const int l = size(s) / 2;
string p = s.substr(0, size(s) - l);
for (int i = 0; i < l; ++i) {
p.push_back(s[(l - 1) - i]);
}
return ((1 << l) - 1) + (n >> l) + (p <= s ? 1 : 0);
}
};