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

这道题是之前那道 4Sum 的延伸,让我们在四个数组中各取一个数字,使其和为0。那么坠傻的方法就是遍历所有的情况,时间复杂度为 O(n4)。但是既然 Two Sum 那道都能将时间复杂度缩小一倍,那么这道题使用 HashMap 是否也能将时间复杂度降到 O(n2) 呢?答案是肯定的,如果把A和B的两两之和都求出来,在 HashMap 中建立两数之和跟其出现次数之间的映射,那么再遍历C和D中任意两个数之和,只要看哈希表存不存在这两数之和的相反数就行了,参见代码如下:

解法一:

class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
int res = ;
unordered_map<int, int> m;
for (int i = ; i < A.size(); ++i) {
for (int j = ; j < B.size(); ++j) {
++m[A[i] + B[j]];
}
}
for (int i = ; i < C.size(); ++i) {
for (int j = ; j < D.size(); ++j) {
int target = - * (C[i] + D[j]);
res += m[target];
}
}
return res;
}
};

下面这种方法用了两个 HashMap 分别记录 AB 和 CB 的两两之和出现次数,然后遍历其中一个 HashMap,并在另一个 HashMap 中找和的相反数出现的次数,参见代码如下:

解法二:

class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
int res = , n = A.size();
unordered_map<int, int> m1, m2;
for (int i = ; i < n; ++i) {
for (int j = ; j < n; ++j) {
++m1[A[i] + B[j]];
++m2[C[i] + D[j]];
}
}
for (auto a : m1) res += a.second * m2[-a.first];
return res;
}
};

类似题目:

4Sum

参考资料:

https://leetcode.com/problems/4sum-ii/

https://leetcode.com/problems/4sum-ii/discuss/93920/Clean-java-solution-O(n2)

https://leetcode.com/problems/4sum-ii/discuss/93925/Concise-C%2B%2B-11-code-beat-99.5

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 4Sum II 四数之和之二的更多相关文章

  1. [LeetCode] 454. 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  2. [LeetCode] 454. 4Sum II 四数之和II

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  3. 【LeetCode】18.四数之和

    题目描述 18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 t ...

  4. LeetCode 18. 4Sum (四数之和)

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  5. 454 4Sum II 四数相加 II

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

  6. 力扣 ——4Sum (四数之和)python 实现

    题目描述: 中文: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 targe ...

  7. 18. 4Sum[M]四数之和

    题目 Given an array nums of n integers and an integer target, are there elements a, b, c and d in nums ...

  8. 【LeetCode 18】四数之和

    题目链接 [题解] 两重循环枚举[i..j]这个区间 同时规定必取nums[i]和nums[j] 那么现在的问题就变成在下标为[i..j]这个区间的数字里面找两个数字使他们的和为target-nums ...

  9. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

随机推荐

  1. 11gRAC报错CRS-4535, CRS-4000解决

    环境:AIX6.1 + Oracle11.2.0.4 RAC(2 nodes) 1.故障现象 2.定位问题 3.处理问题 1.故障现象 使用crsctl查看集群各资源状态,在任一节点都会直接报错CRS ...

  2. Javascript单例模式概念与实例

    前言 和其他编程语言一样,Javascript同样拥有着很多种设计模式,比如单例模式.代理模式.观察者模式等,熟练运用Javascript的设计模式可以使我们的代码逻辑更加清晰,并且更加易于维护和重构 ...

  3. xmpp

    xmpp学习 下载: Openfire 服务器:Openfire 4.0.2 客户端:Spark 2.7.7 安装 Openfire安装: 根据提示一直下一步,服务器域名设置为:localhost(p ...

  4. [C1] 实现 C1FlexGrid 撤销还原功能

    采用设计模式中的"命令模式"实现 C1FlexGrid 的撤销还原功能,那就先从命令模式简单介绍开始吧. 一  命令模式 命令模式属于对象的行为型模式,将一个请求封装为一个对象,从 ...

  5. Error:Execution failed for task ':app:clean'.

    运行时出现 Error:Execution failed for task ':app:clean'. 错误,Builld->Clean Project即可.

  6. Java微信公众平台接口封装源码分享

    前言:      这篇博客是在三月初动手项目的时候准备写的,但是为了完成项目只好拖延时间写这篇博客,顺便也可以在项目中应用我自己总结的的一些经验.今天看来,这些方法的应用还是可以的,至少实现了我之前的 ...

  7. JavaMail发送邮件第一版

    首先,我们先来了解一个基本的知识点,用什么工具来发邮件? 简单的说一下,目前用的比较多的客户端:OutLook,Foxmail等 顺便了解一下POP3.SMTP协议的区别: POP3,全名为" ...

  8. querySelector系列方法相比 getElementsBy 系列方法有什么区别?

    querySelector 和  querySelectorAll 相比下面这些方法有什么区别? getElementsByTagName getElementsByClassName getElem ...

  9. 便于开发的Helper类

    一.将config封装实体层: 例子config: <?xml version="1.0" encoding="utf-8" ?> <Sett ...

  10. Atitit.软件兼容性原理与实践 v5 qa2.docx

    Atitit.软件兼容性原理与实践   v5 qa2.docx 1. Keyword2 2. 提升兼容性的原则2 2.1. What 与how 分离2 2.2. 老人老办法,新人新办法,只新增,少修改 ...