leetcode982 Triples with Bitwise AND Equal To Zero

思路:

使用unordered_map暴力枚举。

实现:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 class Solution
 4 {
 5 public:
 6     int countTriplets(vector<int>& A)
 7     {
 8         unordered_map<int, int> mp;
 9         int n = A.size();
10         for (int i = 0; i < n; i++)
11         {
12             for (int j = 0; j < n; j++)
13             {
14                 int tmp = A[i] & A[j];
15                 if (!mp.count(tmp)) mp[tmp] = 0;
16                 mp[tmp]++;
17             }
18         }
19         int ans = 0;
20         for (int i = 0; i < n; i++)
21         {
22             for (auto it: mp)
23                 if ((A[i] & it.first) == 0) ans += it.second;
24             
25         }
26         return ans;
27     }
28 };
29 int main()
30 {
31     int a[] = {2, 1, 3};
32     vector<int> v(begin(a), end(a));
33     cout << Solution().countTriplets(v) << endl;
34     return 0;
35 }
原文地址:https://www.cnblogs.com/wangyiming/p/10663930.html