题目链接:https://leetcode.cn/problems/triples-with-bitwise-and-equal-to-zero/
# 解题思路
枚举:使用 hash 表 + 子集枚举进行优化
具体思路请点这里
# 代码
class Solution {  | |
public int countTriplets(int[] nums) {  | |
int n = nums.length;  | |
        // 数组代表哈希表 | |
int[] cntMap = new int[1 << 16]; // 65536  | |
        // 枚举前两个数 | |
for (int a : nums) {  | |
for (int b : nums) {  | |
++cntMap[a & b];  | |
            } | |
        } | |
        // 0 & 任意数都是 0 | |
int res = n * cntMap[0];  | |
        // 枚举第三个数 | |
for (int c : nums) {  | |
c = c ^ 0xffff;  | |
for (int k = c; k != 0; k = (k - 1) & c) {  | |
res += cntMap[k];  | |
            } | |
        } | |
return res;  | |
    } | |
} |