Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
triples-with-bitwise-and-equal-to-zero.cpp 1.47 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2019-01-27 19:51 +08:00 . Update triples-with-bitwise-and-equal-to-zero.cpp
// Time: O(nlogn), n is the max of A
// Space: O(n)
// Reference: https://blog.csdn.net/john123741/article/details/76576925
// FWT solution
class Solution {
public:
int countTriplets(vector<int>& A) {
static const int k = 3;
const auto max_A = *max_element(A.cbegin(), A.cend());
int n = 1;
for (; n <= max_A; n <<= 1);
vector<int> B(n);
for (const auto& x : A) {
++B[x];
}
FWT(&B, 1);
for(auto& x : B) {
x = pow(x, k);
}
FWT(&B, -1);
return B[0];
}
private:
void FWT(vector<int> *A, int v) {
for (int d = 1; d < A->size(); d <<= 1) {
for (int m = d << 1, i = 0; i < A->size(); i += m) {
for (int j = 0; j < d ; ++j) {
(*A)[i + j] += (*A)[i + j + d] * v;
}
}
}
}
};
// Time: O(n^3), n is the length of A
// Space: O(n^2)
class Solution2 {
public:
int countTriplets(vector<int>& A) {
unordered_map<int, int> count;
for (int i = 0 ;i < A.size(); ++i) {
for (int j = 0; j < A.size(); ++j) {
++count[A[i] & A[j]];
}
}
int result = 0;
for (int k = 0; k < A.size(); ++k) {
for (const auto& kvp : count) {
if ((A[k] & kvp.first) == 0) {
result += kvp.second;
}
}
}
return result;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助