454. 4Sum II

https://leetcode.com/problems/4sum-ii/

solution

class Solution:
    def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
        dict1 = {}
        for i in nums1:
            for j in nums2:
                if i + j in dict1:
                    dict1[i+j] += 1
                else:
                    dict1[i+j] = 1

        res = 0
        for m in nums3:
            for n in nums4:
                if -m-n in dict1:
                    res += dict1[-m-n]
        return res

时间复杂度:O(n^2) 空间复杂度:O(n^2)

Last updated