An easy cpp solution


  • 0
    Z
    #include <unordered_map>
    class Solution {
    public:
        int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
            int cnt = 0;
            std::unordered_map<int, int> record;
            for (auto i: A) {
                for (auto j: B) {
                    int ab = i + j;
                    if (record.find(ab) == record.end()) {
                        record[ab] = 1;
                    } else {
                        ++record[ab];
                    }
                }
            }
            for (auto k: C) {
                for (auto l: D) {
                    int cd = -k - l;
                    if (record.find(cd) != record.end()) {
                        cnt += record[cd];
                    }
                }
            }
            return cnt;
        }
    };
    

Log in to reply
 

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