• LeetCode_哈希表_中等_454.四数相加 II


    1.题目

    给你四个整数数组 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
    解释:
    两个元组如下:
    (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
    (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

    来源:力扣(LeetCode)
    链接:https://leetcode.cn/problems/4sum-ii

    2.思路

    (1)暴力穷举法
    该方法比较简单也易于想到,使用 4 个 for 循环穷举所有元组的组合并进行判断即可,使用变量 res 来记录符合条件的元组。但是该方法时间复杂度太高(为O(n4)),显然这在LeetCode 上提交时会给出“超出时间限制”的提示!

    (2)哈希表_分组
    思路参考本题官方题解

    3.代码实现(Java)

    //思路1————暴力穷举法
    class Solution {
        public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
            int n = nums1.length;
            int res = 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) {
                                res++;
                            }
                        }
                    }
                }
            }
            return res;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    //思路2————哈希表_分组
    class Solution {
        public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
            int res = 0;
            Map<Integer, Integer> cnt12 = new HashMap<>();
            for (int num1 : nums1) {
                for (int num2 : nums2) {
                    cnt12.put(num1 + num2, cnt12.getOrDefault(num1 + num2, 0) + 1);
                }
            }
            for (int num3 : nums3) {
                for (int num4 : nums4) {
                    if (cnt12.containsKey(-num3 - num4)) {
                        res += cnt12.get(-num3 - num4);
                    }
                }
            }
            return res;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    深度学习之基于Tensorflow卷积神经网络验证码识别系统
    Qt系列-QSplitter使用笔记
    网络编程概述及Http协议
    7 C控制语句:分支和跳转
    阿里云 —— Windows下“阿里云音视频通信RTC“ 之 云端录制编译C++的SDK接口
    Mxnet框架使用
    博客园商业化之路-众包平台:偶遇外包项目需求
    MATLB|电动车智能充电模式及电力高峰需求预测
    详解C#中的命名空间
    【appium】Hybrid应用自动化|微信小程序自动化
  • 原文地址:https://blog.csdn.net/weixin_43004044/article/details/125439346