美文网首页
454. 4Sum II

454. 4Sum II

作者: xiaoyaook | 来源:发表于2017-12-10 13:05 被阅读0次

运用二分法的思想,四个数组A,B,C,D.
A+B进行统计,-C-D进行统计.这样就将问题化简.
调用标准库collectionsCounter方法,可以非常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)

相关文章

网友评论

      本文标题:454. 4Sum II

      本文链接:https://www.haomeiwen.com/subject/gaamixtx.html