/* * @lc app=leetcode id=454 lang=javascript * * [454] 4Sum II * * https://leetcode.com/problems/4sum-ii/description//** * @param {number[]} A * @param {number[]} B * @param {number[]} C * @param {number[]} D * @return {number} */varfourSumCount=function (A, B, C, D) {constsumMapper= {};let res =0;for (let i =0; i <A.length; i++) {for (let j =0; j <B.length; j++) { sumMapper[A[i] +B[j]] = (sumMapper[A[i] +B[j]] ||0) +1; } }for (let i =0; i <C.length; i++) {for (let j =0; j <D.length; j++) { res += sumMapper[-(C[i] +D[j])] ||0; } }return res;};
Python3:
classSolution:deffourSumCount(self,A: List[int],B: List[int],C: List[int],D: List[int]) ->int: mapper ={} res =0for i in A:for j in B: mapper[i + j]= mapper.get(i + j, 0)+1for i in C:for j in D: res += mapper.get(-1* (i + j), 0)return res