leetcode-代码随想录-哈希表-四数相加Ⅱ

题目

题目链接:454. 四数相加 II - 力扣(LeetCode)

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

  • 0 <= i, j, k, l < n

  • nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0

    输入: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

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

c++ 复制代码
class Solution {
public:
    int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
        
    }
};
思路 & 代码
map 作哈希表
  • 首先定义 一个unordered_map,key放a和b两数之和,value 放a和b两数之和出现的次数。

  • 遍历大A和大B数组,统计两个数组元素之和,和出现的次数,放到map中。

  • 定义int变量count,用来统计 a+b+c+d = 0 出现的次数。

  • 再遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就用count把map中key对应的value也就是出现次数统计出来。

  • 最后返回统计值 count 就可以了

  • 问题1:为什么先遍历两个数组,再遍历两个数组,而不是先遍历一个数组,再遍历三个数组?

    • 先遍历两个,时间复杂度是O(n ^ 2),再遍历两个,时间复杂度是O(n ^ 2),所以最终时间复杂度是O(n ^ 2);
    • 若遍历一个,时间复杂度是O(n ),再遍历三个,时间复杂度是O(n ^ 3),所以最终时间复杂度是O(n ^ 3)。
    • 所以先遍历两个,再遍历两个,效率更高。
  • 问题2 : 为什么 count += value,而不是 count++?

代码
c++ 复制代码
#include <vector>
#include <iostream>
#include <sstream>
#include <unordered_map>
using namespace std;


class Solution {
    public:
        int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
            unordered_map<int, int> myMap;
            for(int a : nums1){
                for(int b : nums2){
                    myMap[a + b]++; // 关键操作1:记录 a+b 的出现次数
                }
            }

            int count = 0;
            for(int c : nums3){
                for(int d : nums4){
                    int target = 0 - (c + d); // 需要找到的互补值
                    if(myMap.find(target) != myMap.end()){ // 关键操作2:查找互补值
                        count += myMap[target]; // 关键操作3:累加出现次数
                    }
                }
            }
            return count;
        }
};


int main() {
    int n;
    cin >> n;

    vector<int> nums1(n), nums2(n), nums3(n), nums4(n);

    cout << "nums1: ";
    for(int i = 0; i < n; i++){
        cin >> nums1[i];
    }

    cout << "nums2: ";
    for(int i = 0; i < n; i++){
        cin >> nums2[i];
    }

    cout << "nums3: ";
    for(int i = 0; i < n; i++){
        cin >> nums3[i];
    }

    cout << "nums4: ";
    for(int i = 0; i < n; i++){
        cin >> nums4[i];
    }

    Solution obj;
    int res = obj.fourSumCount(nums1, nums2, nums3, nums4);
    cout << "res: " << res << endl;

    return 0;
}
相关推荐
vibecoding日记10 小时前
双非如何快速入职字节等大厂大模型?真实案例分析:推理优化和投机解码
算法·求职·大模型工程师
yszaygr213813 小时前
Verilog参数化游程编码RLE模块
算法
望易13 小时前
刚设计的大模型架构-双域耦合认知框架
算法·架构
复杂网络17 小时前
多个 Claude Code 与多个 Codex 协同工作:设计与实现方案
算法
HjhIron1 天前
面试常客:字符串算法从入门到进阶
算法·面试
吴佳浩1 天前
DeepSeek DSpark:Confidence-Scheduled Speculative Decoding 技术解析
人工智能·算法·deepseek
触底反弹1 天前
🧠 搞懂 Token,才算真正入门大模型——从分词原理到 Embedding 语义实战
javascript·人工智能·算法
vivo互联网技术2 天前
ICLR 2026 | 基于后验采样的图像恢复方法LearnIR:人脸去阴影、去雾
人工智能·算法·aigc
浮生望2 天前
JS字符串与回文算法:从包装类到双指针的面试进阶之路
javascript·算法