mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
update 四数相加II,添加了一个更加简洁的python代码
This commit is contained in:
@ -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方法会返回None(key不存在)或者key对应的value
|
||||
# 所以如果value==0,就会继续执行or,count+0,否则就会直接加value
|
||||
# 这样就不用去写if判断了
|
||||
|
||||
# count += value or 0
|
||||
|
||||
# return count
|
||||
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user