diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index a7c903eb..a6cd413b 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -141,7 +141,24 @@ class Solution(object): ``` +```python +class Solution: + def fourSumCount(self, nums1: list, nums2: list, nums3: list, nums4: list) -> int: + from collections import defaultdict # You may use normal dict instead. + rec, cnt = defaultdict(lambda : 0), 0 + # To store the summary of all the possible combinations of nums1 & nums2, together with their frequencies. + for i in nums1: + for j in nums2: + rec[i+j] += 1 + # To add up the frequencies if the corresponding value occurs in the dictionary + for i in nums3: + for j in nums4: + cnt += rec.get(-(i+j), 0) # No matched key, return 0. + return cnt +``` + Go: + ```go func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int { m := make(map[int]int)