mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 04:54:51 +08:00
Added in one more python solution. Using defaultdict.
This commit is contained in:
@ -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)
|
||||
|
Reference in New Issue
Block a user