LeetCode 454. 4Sum II

原创 2018年04月15日 21:38:06

题目描述

  • 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 -2^28 to 2^28 - 1 and the result is guaranteed to be at most 2^31 - 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

问题分析

  • 若四层循环遍历四个数组,这样是 O(N^4),超时
  • 若将一个数组装入map,然后三层循环遍历三个数组,再去根据和去查表,O(N^3),超时
  • 注意题干所给数据,500,所以要求设计的算法一定是 小于 O(N^3)的。
  • 正确做法,两层循环遍历 C,D,将所有可能的和放入map,并统计次数,然后两层遍历 A,B,再去查表即可。 O(N^2)

经验教训

  • 如何根据数据规模推算出所要设计的算法时间复杂度的上限

代码实现

class Solution {
    public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
        if (A== null || B == null || C == null || D == null || A.length == 0 || B.length == 0 || C.length == 0 || D.length == 0) {
            return 0;
        }
        //将C与D的所有可能组合装入map中
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int numC : C) {
            for (int numD : D) {
                map.put(numC + numD, map.getOrDefault(numC + numD, 0) + 1);
            }
        }
        int res = 0;
        //遍历A,B,看在map中能否找到相应的组合,使四个数相加为0
        for (int numA : A) {
            for (int numB : B) {
                res +=  map.getOrDefault(0 - numA - numB, 0);
            }
        }
        return res;
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zjxxyz123/article/details/79953684

Leetcode 454. 4Sum II 四数之和2 解题报告

1 解题思想首先,这是一道远古之前的题的进化版: Leetcode #18 4Sum 四数之和 解题小节+K-Sum思想 但是我不想说那个题了,因为我也记不住了。。这道题意思就是ABCD四个数组,长...
  • MebiuW
  • MebiuW
  • 2016-11-16 22:45:38
  • 3385

【leetcode】454. 4Sum II【M】

Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such...
  • sscssz
  • sscssz
  • 2016-11-22 13:11:12
  • 574

LeetCode 454 4Sum II 题解

题意简述:给出A、B、C、D四个区间,分别从这四个区间取出一个整数,使其相加得到的和等于0,求出总共有多少种方式。 输入:四个整型的vectorA、B、C、D 输出:满足条件的四元组(i,j,k,...
  • MonoTali
  • MonoTali
  • 2017-03-12 19:33:58
  • 321

454. 4Sum II

4个数的和
  • u014376961
  • u014376961
  • 2016-11-13 13:16:21
  • 495

LeetCode--4Sum II(4个数的和)Python

题目: 给定四个长度为N的数组:A、B、C、D,找到A[i]+B[j]+C[k]+D[l]=0的tuple个数。 解题思路: 直接对四个数组进行遍历,时间复杂度为N的4次方,超时。之后想到将哈希...
  • xiaoxiaoley
  • xiaoxiaoley
  • 2018-01-23 10:54:40
  • 106

[leetcode] 454. 4Sum II 解题报告

题目链接:https://leetcode.com/problems/4sum-ii/ Given four lists A, B, C, D of integer values, comp...
  • qq508618087
  • qq508618087
  • 2016-11-21 08:54:49
  • 901

Leetcode 454. 4Sum II

public class Solution { public int fourSumCount(int[] A, int[] B, int[] C, int[] D) { in...
  • u011094114
  • u011094114
  • 2017-01-13 05:54:20
  • 113

[leetcode]454. 4Sum II

题目链接:https://leetcode.com/problems/4sum-ii/ Given four lists A, B, C, D of integer values, comp...
  • xiaocong1990
  • xiaocong1990
  • 2017-01-10 18:16:43
  • 135

[Leetcode] 454. 4Sum II

Description: Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) th...
  • Peggy_Chang
  • Peggy_Chang
  • 2018-03-18 21:23:06
  • 8

【Leetcode】454. 4Sum II

问题: 454. 4Sum II Given four lists A, B, C, D of integer values, compute how many tuples (i, ...
  • zhizhi_zhizhi_zhizhi
  • zhizhi_zhizhi_zhizhi
  • 2017-01-12 20:36:06
  • 86
收藏助手
不良信息举报
您举报文章:LeetCode 454. 4Sum II
举报原因:
原因补充:

(最多只允许输入30个字)