update 四数相加II,添加了一个更加简洁的python代码

This commit is contained in:
borninfreedom
2021-06-15 16:01:10 +08:00
parent 4993e9ff6b
commit e1bbd30d8e

View File

@ -120,7 +120,7 @@ class Solution {
Python
```
```python
class Solution(object):
def fourSumCount(self, nums1, nums2, nums3, nums4):
"""
@ -147,7 +147,31 @@ class Solution(object):
if key in hashmap:
count += hashmap[key]
return count
# 下面这个写法更为简洁,但是表达的是同样的算法
# class Solution:
# def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
# from collections import defaultdict
# hashmap = defaultdict(int)
# for x1 in nums1:
# for x2 in nums2:
# hashmap[x1+x2] += 1
# count=0
# for x3 in nums3:
# for x4 in nums4:
# key = -x3-x4
# value = hashmap.get(key)
# dict的get方法会返回Nonekey不存在或者key对应的value
# 所以如果value==0就会继续执行orcount+0否则就会直接加value
# 这样就不用去写if判断了
# count += value or 0
# return count
```