运用二分法的思想,四个数组A,B,C,D.
A+B进行统计,-C-D进行统计.这样就将问题化简.
调用标准库collections
的Counter
方法,可以非常Pythonic.
也可以自己用字典实现,只是代码长一点.
class Solution(object):
def fourSumCount(self, A, B, C, D):
"""
:type A: List[int]
:type B: List[int]
:type C: List[int]
:type D: List[int]
:rtype: int
"""
ab = collections.Counter(a+b for a in A for b in B)
return sum(ab[-c-d] for c in C for d in D)
网友评论