• 哈希表6——四数相加


    例题

    力扣题目链接:

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

    题目说明

    给你四个整数数组 nums1nums2nums3nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足:

    • 0 ≤ i , j , k , l < n 0 \leq i, j, k, l < n 0i,j,k,l<n
    • n u m s 1 [ i ] + n u m s 2 [ j ] + n u m s 3 [ k ] + n u m s 4 [ l ] = = 0 nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0 nums1[i]+nums2[j]+nums3[k]+nums4[l]==0

    示例 1:

    输入:nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
    输出:2
    解释:
    两个元组如下:
    1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
    2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    示例 2:

    输入:nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
    输出:1
    
    • 1
    • 2

    提示

    • n == nums1.length
    • n == nums2.length
    • n == nums3.length
    • n == nums4.length
    • 1 ≤ n ≤ 200 1 \leq n \leq 200 1n200
    • − 2 28 ≤ n u m s 1 [ i ] , n u m s 2 [ i ] , n u m s 3 [ i ] , n u m s 4 [ i ] ≤ 2 28 -2^{28} \leq nums1[i], nums2[i], nums3[i], nums4[i] \leq 2^{28} 228nums1[i],nums2[i],nums3[i],nums4[i]228

    解题

    方法一:分组 + 哈希

    思路与算法

    我们可以将四个数组分成两部分, A A A B B B 为一组, C C C D D D 为另外一组。

    对于 A A A B B B,我们使用二重循环对它们进行遍历,得到所有 A [ i ] + B [ j ] A[i]+B[j] A[i]+B[j] 的值并存入哈希映射中。对于哈希映射中的每个键值对,每个键表示一种 A [ i ] + B [ j ] A[i]+B[j] A[i]+B[j],对应的值为 A [ i ] + B [ j ] A[i]+B[j] A[i]+B[j] 出现的次数。

    对于 C C C D D D,我们同样使用二重循环对它们进行遍历。当遍历到 C [ k ] + D [ l ] C[k]+D[l] C[k]+D[l]时,如果 − ( C [ k ] + D [ l ] ) -(C[k]+D[l]) (C[k]+D[l])出现在哈希映射中,那么将 − ( C [ k ] + D [ l ] ) -(C[k]+D[l]) (C[k]+D[l]) 对应的值累加进答案中。

    最终即可得到满足 A [ i ] + B [ j ] + C [ k ] + D [ l ] = 0 A[i]+B[j]+C[k]+D[l]=0 A[i]+B[j]+C[k]+D[l]=0 的四元组数目。

    代码

    c++

    class Solution {
    public:
        int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
            unordered_map<int, int> countAB;
            for (int u: A) {
                for (int v: B) {
                    ++countAB[u + v];
                }
            }
            int ans = 0;
            for (int u: C) {
                for (int v: D) {
                    if (countAB.count(-u - v)) {
                        ans += countAB[-u - v];
                    }
                }
            }
            return ans;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    Python3

    class Solution:
        def fourSumCount(self, A: List[int], B: List[int], C: List[int], D: List[int]) -> int:
            countAB = collections.Counter(u + v for u in A for v in B)
            ans = 0
            for u in C:
                for v in D:
                    if -u - v in countAB:
                        ans += countAB[-u - v]
            return ans
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    复杂度分析

    时间复杂度 O ( n 2 ) O(n^2) O(n2)。我们使用了两次二重循环,时间复杂度均为 O ( n 2 ) O(n^2) O(n2)
    。在循环中对哈希映射进行的修改以及查询操作的期望时间复杂度均为 O ( 1 ) O(1) O(1),因此总时间复杂度为 O ( n 2 ) O(n^2) O(n2)

    空间复杂度 O ( n 2 ) O(n^2) O(n2),即为哈希映射需要使用的空间。在最坏的情况下, A [ i ] + B [ j ] A[i]+B[j] A[i]+B[j] 的值均不相同,因此值的个数为 n 2 n^2 n2 ,也就需要 O ( n 2 ) O(n^2) O(n2)的空间。

  • 相关阅读:
    STL库->String(C++)
    数据链路层
    Java整合t-io实现在线聊天功能
    漏洞深度分析|Apache Airflow example_bash_operator DAG 远程代码执行漏洞
    Android---StartActivity启动过程
    scrapy简单实现一个项目
    Git精讲
    【Hydro】水文模型比较框架MARRMoT - 包含47个概念水文模型的Matlab代码
    基于正交分解的室外阴影图像恢复
    计算机网络物理层思维导图+大纲笔记
  • 原文地址:https://blog.csdn.net/wtlll/article/details/125439013