Day89 四数相加 II

给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0

[ 454. 四数相加 II - 力扣(LeetCode) (leetcode-cn.com) ](https://leetcode-cn.com/problems/4sum-ii/)

示例1:

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

输出:
2

解释:
两个元组如下:

  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

提示:

为了使问题简单化,所有的 A, B, C, D 具有相同的长度 N,且 0 ≤ N ≤ 500 。所有整数的范围在 -228 到 228 - 1 之间,最终结果不会超过 231 - 1 。

Java解法

思路:

  • 思路1 :最简单的暴力操作:n^4 遍历,必定超时
  • 思路2 :优化上述处理,使用HashMap储存两组数据遍历之和,再对两个HashMap遍历处理 (2N)^2
    竟然过了,应该是用例数据有限制,在限制条件下完成功能了
package sj.shimmer.algorithm.m4_2021;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by SJ on 2021/4/26.
 */

class D89 {
    public static void main(String[] args) {
        int[] nums1 = new int[]{1, 2};
        int[] nums2 = new int[]{-2,-1};
        int[] nums3 = new int[]{-1, 2};
        int[] nums4 = new int[]{0, 2};
        System.out.println(fourSumCount(nums1,nums2,nums3,nums4));
    }
    public static int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        int result = 0;
        HashMap<Integer,Integer> map1 = addNums(nums1,nums2);
        HashMap<Integer,Integer> map2 = addNums(nums3,nums4);
        for (Map.Entry<Integer, Integer> entry1 : map1.entrySet()) {
            for (Map.Entry<Integer, Integer> entry2 : map2.entrySet()) {
                if (entry1.getKey()+entry2.getKey()==0) {
                    result += entry1.getValue()*entry2.getValue();
                }
            }
        }
        return result;
    }
    public static HashMap<Integer,Integer> addNums(int[] n1,int[] n2){
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i : n1) {
            for (int j : n2) {
                int r = i + j;
                Integer size = map.getOrDefault(r, 0);
                map.put(r, ++size);
            }
        }
        return map;
    }
}

官方解

[ 四数相加 II - 四数相加 II - 力扣(LeetCode) (leetcode-cn.com) ](https://leetcode-cn.com/problems/4sum-ii/solution/si-shu-xiang-jia-ii-by-leetcode-solution/)

  1. 分组 + 哈希表

    emmm,竟然跟我的处理类似,emmm 以后找到更优解再来

class Solution {
    public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
        Map<Integer, Integer> countAB = new HashMap<Integer, Integer>();
        for (int u : A) {
            for (int v : B) {
                countAB.put(u + v, countAB.getOrDefault(u + v, 0) + 1);
            }
        }
        int ans = 0;
        for (int u : C) {
            for (int v : D) {
                if (countAB.containsKey(-u - v)) {
                    ans += countAB.get(-u - v);
                }
            }
        }
        return ans;
    }
}

写法更优雅点,效率上提升了5%

  • 时间复杂度:O(n^2)
  • 空间复杂度:O(n^2)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值