Added in one more python solution. Using defaultdict.

This commit is contained in:
Camille0512
2022-02-24 00:36:23 +08:00
parent b07dba43a8
commit e54ba10fa1

View File

@ -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)