Leetcode:454. 4Sum II (解决时间和空间复杂度是关键)

本文介绍了一种解决四数求和问题的方法,利用HashMap优化了时间和空间复杂度,通过预处理两数组之和并存储频率,实现了快速查找匹配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.

To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1.

Example:

Input:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]

Output:
2

Explanation:
The two tuples are:
1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0

这个题主要解决空间和时间的问题,我的一种错误解法就是控制时间复杂度在n*n但是空间复杂度不满足OJ要求。

错误代码:

public static int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
        int count = 0;
        int index = 0;
        int nums1[] =new int[A.length*B.length];
        int nums2[] =new int[A.length*B.length*C.length];
        int nums3[] =new int[A.length*B.length*C.length*D.length];
        for (int i = 0; i < A.length; i++) {
            for (int j = 0; j < B.length; j++) {
                nums1[index++]=A[i]+B[j];
            }
        }
        index = 0;
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < C.length; j++) {
                nums2[index++]=nums1[i]+C[j];
            }
        }
        index = 0;
        for (int i = 0; i < nums2.length; i++) {
            for (int j = 0; j < D.length; j++) {
                nums3[index++]=nums2[i]+D[j];
            }
        }

        for (int i = 0; i < nums3.length; i++) {
            if (nums3[i]==0) {
                count++;
            }
        }
        for (int i = 0; i < nums1.length; i++) {
            System.out.println(nums1[i]);
        }
        for (int i = 0; i < nums2.length; i++) {
            System.out.println(nums2[i]);
        }
        for (int i = 0; i < nums3.length; i++) {
            System.out.println(nums3[i]);
        }
        return count;

    }

借助HashMap,其实这个非常的好想到,这样想,我们是要来记录两个数字的和,但是如果用数组,也就是上面的情况,那么肯定有重复的值存在,所以现在我们用Map来存值,可以解决时间复杂度不满足的情况,如果sums不存在这个sum,那么就加入,键为sum 值为1 但是我们知道键值对不允许键重复,当存在和为重复的时候,那么我们就判断一下,然后sums加入sum,但是这个值加1,这样就可以统计当前sum的个数,为后面计算元组个数做准备。
之后再第二个循环里面 如果存在-sum那么这个元组就可以,取出值相加就可以了。
———简单清晰。

public static int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
        Map<Integer,Integer> sums = new HashMap<>();
        int count = 0;
        for(int i=0; i<A.length;i++) {
            for(int j=0;j<B.length;j++){
                int sum = A[i]+B[j];
                if(sums.containsKey(sum)) {
                    sums.put(sum, sums.get(sum)+1);
                } else {
                    System.out.println(sum);
                    sums.put(sum, 1);
                }
            }
        }
        //System.out.println(sums.toString());
        for(int k=0; k<A.length;k++) {
            for(int z=0;z<C.length;z++){
                int sum = -(C[k]+D[z]);
                if(sums.containsKey(sum)) {
                    count+=sums.get(sum);
                }
            }
        }
        return count;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值