@@ -47,6 +47,8 @@ The two tuples are:
4747- 两两分组,求出两两结合能够得出的可能数,然后合并即可。
4848
4949## 代码
50+
51+ 语言支持: ` JavaScript ` ,` Python3 `
5052``` js
5153
5254/*
@@ -55,41 +57,6 @@ The two tuples are:
5557 * [454] 4Sum II
5658 *
5759 * https://leetcode.com/problems/4sum-ii/description/
58- *
59- * algorithms
60- * Medium (49.93%)
61- * Total Accepted: 63.2K
62- * Total Submissions: 125.6K
63- * Testcase Example: '[1,2]\n[-2,-1]\n[-1,2]\n[0,2]'
64- *
65- * Given four lists A, B, C, D of integer values, compute how many tuples (i,
66- * j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.
67- *
68- * To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤
69- * N ≤ 500. All integers are in the range of -2^28 to 2^28 - 1 and the result
70- * is guaranteed to be at most 2^31 - 1.
71- *
72- * Example:
73- *
74- *
75- * Input:
76- * A = [ 1, 2]
77- * B = [-2,-1]
78- * C = [-1, 2]
79- * D = [ 0, 2]
80- *
81- * Output:
82- * 2
83- *
84- * Explanation:
85- * The two tuples are:
86- * 1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
87- * 2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
88- *
89- *
90- *
91- *
92- */
9360/**
9461 * @param {number[]} A
9562 * @param {number[]} B
@@ -115,3 +82,18 @@ var fourSumCount = function(A, B, C, D) {
11582 return res;
11683};
11784```
85+
86+ ``` python
87+ class Solution :
88+ def fourSumCount (self , A : List[int ], B : List[int ], C : List[int ], D : List[int ]) -> int :
89+ mapper = {}
90+ res = 0
91+ for i in A:
92+ for j in B:
93+ mapper[i + j] = mapper.get(i + j, 0 ) + 1
94+
95+ for i in C:
96+ for j in D:
97+ res += mapper.get(- 1 * (i + j), 0 )
98+ return res
99+ ```
0 commit comments