454 4Sum II 四數相加 II
Description:
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.
To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -2^28 to 2^28 - 1 and the result is guaranteed to be at most 2^31 - 1.
Example:
Input:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]
Output:
2
Explanation:
The two tuples are:
- (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
- (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
題目描述:
給定四個包含整數的數組列表 A , B , C , D ,計算有多少個元組 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0。
爲了使問題簡單化,所有的 A, B, C, D 具有相同的長度 N,且 0 ≤ N ≤ 500 。所有整數的範圍在 -2^28 到 2^28 - 1 之間,最終結果不會超過 2^31 - 1 。
示例 :
例如:
輸入:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]
輸出:
2
解釋:
兩個元組如下:
- (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
- (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
思路:
用 map記錄 a + b的值, 遍歷 c和 d, 找到 -(c + d)就累計到結果中
時間複雜度O(n ^ 2), 空間複雜度O(n ^ 2)
代碼:
C++:
class Solution
{
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D)
{
unordered_map<int, int> m;
int result = 0, s = 0;
for (auto a : A) for (auto b : B) ++m[a + b];
for (auto c : C) for (auto d : D) if (m.count(s = -(c + d))) result += m[s];
return result;
}
};
Java:
class Solution {
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
Map<Integer, Integer> map = new HashMap<>();
int result = 0;
for (int a : A) for (int b : B) map.put(a + b, map.getOrDefault(a + b, 0) + 1);
for (int c : C) for (int d : D) if (map.containsKey(-(c + d))) result += map.get(-(c + d));
return result;
}
}
Python:
class Solution:
def fourSumCount(self, A: List[int], B: List[int], C: List[int], D: List[int]) -> int:
result, m = 0, dict(collections.Counter([a + b for a in A for b in B]))
for c in C:
for d in D:
s = -(c + d)
if s in m:
result += m[s]
return result