Skip to content

Commit

Permalink
Added in one more python solution. Using defaultdict.
Browse files Browse the repository at this point in the history
  • Loading branch information
Camille0512 committed Feb 23, 2022
1 parent b07dba4 commit e54ba10
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions problems/0454.四数相加II.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e54ba10

Please sign in to comment.