[LeetCode] 454. 四数相加 II

img

1. 问题描述

454. 四数相加 II - 力扣(LeetCode)

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

  • 0 <= i, j, k, l < n
  • 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

示例 2:

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

提示:

  • n == nums1.length
  • n == nums2.length
  • n == nums3.length
  • n == nums4.length
  • 1 <= n <= 200
  • 228 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 228

2. 实现思路

2.1 自己的思路

~~(一定要写出来啊喂)~

  • 暴力解
/*
 * @lc app=leetcode.cn id=454 lang=csharp
 *
 * [454] 四数相加 II
 */

// @lc code=start
public class Solution
{
    public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4)
    {
        int n = nums1.Length;
        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                for (int k = 0; k < n; k++)
                {
                    for (int l = 0; l < n; l++)
                    {
                        if (nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0) ans++;
                    }
                }
            }
        }

        return ans;
    }
}
// @lc code=end

但是在遇到四个数组都为0的较大数据规模时爆TLE,有想到用哈希将每个数组去重,然后每种元组只需要+对应相同元素个数即可(元素等价避免重复计算)

有点类似第一题两数之和的思想

2.2 题解思路

第454题.四数相加II | 哈希表 | map | 统计查找

  1. 首先定义 一个unordered_map,key放a和b两数之和,value 放a和b两数之和出现的次数。
  2. 遍历大A和大B数组,统计两个数组元素之和,和出现的次数,放到map中。
  3. 定义int变量count,用来统计 a+b+c+d = 0 出现的次数。
  4. 再遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就用count把map中key对应的value也就是出现次数统计出来。
  5. 最后返回统计值 count 就可以了

算count确实和我之前的思路相像,但没想到将四个数组化成两个两个思考,这样拆解后子问题就类似两数之和了

C++:

class Solution {
public:
    int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
        unordered_map<int, int> umap; //key:a+b的数值,value:a+b数值出现的次数
        // 遍历大A和大B数组,统计两个数组元素之和,和出现的次数,放到map中
        for (int a : A) {
            for (int b : B) {
                umap[a + b]++;
            }
        }
        int count = 0; // 统计a+b+c+d = 0 出现的次数
        // 再遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就把map中key对应的value也就是出现次数统计出来。
        for (int c : C) {
            for (int d : D) {
                if (umap.find(0 - (c + d)) != umap.end()) {
                    count += umap[0 - (c + d)];
                }
            }
        }
        return count;
    }
};
  • 时间复杂度: O(n^2)
  • 空间复杂度: O(n^2),最坏情况下A和B的值各不相同,相加产生的数字个数为 n^2

C#:

public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
       Dictionary<int, int> dic = new Dictionary<int, int>();
        foreach(var i in nums1){
            foreach(var j in nums2){
                int sum = i + j;
                if(dic.ContainsKey(sum)){
                    dic[sum]++;
                }else{
                    dic.Add(sum, 1);
                }
                
            }
        }
        int res = 0;
         foreach(var a in nums3){
            foreach(var b in nums4){
                int sum = a+b;
                if(dic.TryGetValue(-sum, out var result)){
                    res += result;
                }
            }
        }
        return res;
    }

2.3 优化(或者敲一遍题解?)

/*
 * @lc app=leetcode.cn id=454 lang=csharp
 *
 * [454] 四数相加 II
 */

// @lc code=start
public class Solution
{
    public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4)
    {
        int ans = 0;
        var dict = new Dictionary<int, int>();
        foreach (var a in nums1)
        {
            foreach (var b in nums2)
            {
                int sum = a + b;
                if (dict.ContainsKey(sum))
                    dict[sum]++;
                else
                    dict.Add(sum, 1);
            }
        }

        foreach (var c in nums3)
        {
            foreach (var d in nums4)
            {
                int sum = c + d;
                if (dict.TryGetValue(-sum, out int count))
                    ans += count;
            }
        }

        return ans;
    }
}
// @lc code=end


3. 补充知识と杂谈,总结

posted @ 2026-08-09 10:56  绘星tsuki  阅读(4)  评论(0)    收藏  举报