Triples with Bitwise AND Equal To Zero Solutions in C++
Number 982
Difficulty Hard
Acceptance 55.5%
Link LeetCode
Other languages —
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/triples-with-bitwise-and-equal-to-zero/// Author : Hao Chen// Date : 2020-07-26class Solution {public:int countTriplets(vector<int>& A) {int n = A.size();//using a map to aggregate the duplicationunordered_map<int, int> rec;for (int i=0; i<n; i++) {for (int j=0; j<n; j++) {rec[A[i] & A[j]]++;}}int result = 0;for (auto &r : rec ) {for (int k=0; k<n; k++) {if ((r.first & A[k]) == 0) result+=r.second;}}return result;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/triples-with-bitwise-and-equal-to-zero//// Author : liuyubobobo/// Time : 2019-01-26#include <iostream>#include <vector>using namespace std;/// Using HashSet/// Time Complexity: O(max(2^16 * n, n^2))/// Space Complexity: O(2^16 * n)class Solution {public:int countTriplets(vector<int>& A) {int n = A.size();vector<int> table(65536, 0);for(int a: A)for(int i = 0; i < 65536; i ++)if(!(a & i))table[i] ++;int res = 0;for(int i = 0; i < n; i ++)for(int j = 0; j < n; j ++)res += table[A[i] & A[j]];return res;}};int main() {return 0;}