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

在给定的4个数组中各取1个数字,使4个数的和为0,返回找到的次数。

如果用暴力搜索Bruce Force,那么Time: O(n^4),用时太长。

好的解法是用Hash map,建立一个map,将A,B中每两个数的和作为key,次数作为value写入map,然后查找C,D中每两个数和的相反数是否在map中,如果在,就把 map 中记录的次数累加到结果中。

Java:

public class Solution {
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
int res = 0;
HashMap<Integer,Integer> ab = new HashMap<Integer,Integer>(); for(int a:A){
for(int b:B){
ab.put(a+b,ab.getOrDefault(a+b,0) + 1);
}
}
for(int c:C){
for(int d:D){
int part2 = c + d;
int part1 = - part2;
res += ab.getOrDefault(part1,0);
}
}
return res;
}
}

Python:

class Solution(object):
def fourSumCount(self, A, B, C, D):
ans = 0
cnt = collections.defaultdict(int)
for a in A:
for b in B:
cnt[a + b] += 1
for c in C:
for d in D:
ans += cnt[-(c + d)]
return ans

Python: 这个写法牛逼到了只用两行就搞定了

class Solution(object):
def fourSumCount(self, A, B, C, D):
A_B_sum = collections.Counter(a+b for a in A for b in B)
return sum(A_B_sum[-c-d] for c in C for d in D)

C++:

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

    

类似题目:

[LeetCode] 18. 4Sum 四数之和

All LeetCode Questions List 题目汇总

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

  1. 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 ...

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

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

  3. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

  4. Leetcode(18)-四数之和

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...

  5. [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 ...

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

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

  7. 454 4Sum II 四数相加 II

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

  8. C#LeetCode刷题之#167-两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3903 访问. 给定一个已按照升序排列 的有序数组,找到两个数使得 ...

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

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

随机推荐

  1. 《逆袭团队》第九次团队作业【Beta】Scrum meeting 2

    项目 内容 软件工程 任课教师博客主页链接 作业链接地址 团队作业9:Beta冲刺与团队项目验收 团队名称 逆袭团队 具体目标 (1)掌握软件黑盒测试技术:(2)学会编制软件项目总结PPT.项目验收报 ...

  2. 运行pyqt4生成py文件增加代码

    if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) Form = QtGui.QWid ...

  3. re正则match、search、findall、finditer函数方法使用

    match 匹配string 开头,成功返回Match object, 失败返回None,只匹配一个. search 在string中进行搜索,成功返回Match object, 失败返回None, ...

  4. centos7安装yum安装pip

    pip是python中的一个包管理工具,可以对Python包的查找.下载.安装.卸载的作用. yum -y install epel-release yum -y install python-pip ...

  5. Tensorflow细节-P290-命名空间与tensorboard上的节点

    讲解几个重点知识 1.对于tf.get_variable()中的reuse,意思是,如果有名字一模一样的变量,则对这个变量继续使用,如果没有名字一模一样的变量,则创建这个变量 2.options=ru ...

  6. Zotero使用教程(2)-数据备份

    小书匠 这篇文章的目标是让你无论是 换系统,重新安装zotero等都可以还原回你的文献库,而且整个过程基本是自动完成的. 这部分解决下面的两种情况: 1.zotero有自己既定的一套存储方式,不是一般 ...

  7. python下浏览器静默运行驱动

    此处以chromdriver为例,放置driver路径问题参看上一篇问题.和java处理差不多,python实现静默运行方式如下 首先解答为什么进行静默运行? 我们在本地一般便于调试可以用GUI界面运 ...

  8. 【整理】Xcode中的iOS模拟器(iOS Simulator)的介绍和使用心得

    [整理]Xcode中的iOS模拟器(iOS Simulator)的介绍和使用心得 iOS模拟器简介 iOS功能简介 iOS模拟器,是在Mac下面开发程序时,开发iOS平台的程序时候,可以使用的辅助工具 ...

  9. 什么是TCP粘包?怎么解决这个问题

    在socket网络编程中,都是端到端通信,由客户端端口+服务端端口+客户端IP+服务端IP+传输协议组成的五元组可以明确的标识一条连接.在TCP的socket编程中,发送端和接收端都有成对的socke ...

  10. 梯形法求解常微分方程(c++)

    #include<iostream> #include<iomanip> using namespace std; int main() { double x,y,yn,h,t ...