【題解】LeetCode 1711. Count Good Meals

【題目敘述】https://leetcode.com/problems/count-good-meals/

class Solution {
public:
    int countPairs(vector<int>& v) {
        unordered_map <int, int> mp;
        long long ans = 0;
        for (auto i:v){
            for (int j = 21; j >= 0; j--){
                if ((1<<j) < i) break;
                if (mp.count((1<<j)-i)) ans += mp[(1<<j)-i];
            }
            mp[i]++;
        }
        int p = 1e9+7;
        return ans%(int)(1e9+7);
    }
};
分享本文 Share with friends