c++ O(n^2log(n))


  • 0
    T
    class Solution {
    public:
        int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
            int sz = A.size();
            unordered_map<int, int> sum;
            for (int i = 0; i < sz; ++i) {
                for (int j = 0; j < sz; ++j) {
                    ++sum[A[i]+B[j]];
                }
            }
            int count = 0;
            for (int i = 0; i < sz; ++i) {
                for (int j = 0; j < sz; ++j) {
                    int resid = -C[i]-D[j];
                    if (sum.find(resid) != sum.end()) count += sum[resid];
                }
            }
            return count;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.