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. django migrate报错:1005 - Can't create table xxx (errno: 150 "Foreign key constraint is incorrectly formed")

    自从mysql升级,以及使用mariaDB以来,很多不曾更新django中model的外键, 今天,按以前的思路写完外键之后, migrate命令报错: 1005 - Can't create tab ...

  2. Kotlin属性引用详解

    继续来学习Kotlin反射相关的,这次主要是跟反射属性相关的东东. 属性引用(Property Reference): 属性引用的用法与函数(方法)引用的用法是完全一致,都是通过::形式来引用的.下面 ...

  3. Calendar.getInstance()获取指定时间点(定时)

    public class Test1 { public static void main(String[]args){ System.out.println("时间为:\n"+ge ...

  4. easyui dialog 设置弹窗位于页面中间

    原文链接:https://my.oschina.net/jingyao/blog/776603 此方法为解决页面含有滚动条时,弹窗位置错误问题,此方法可将带滚动条页面中弹窗显示于页面中间. $(&qu ...

  5. Mybatis分页方法

    使用方法https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md 使用 Maven 在 pom ...

  6. 编程小白入门分享三:Spring AOP统一异常处理

    Spring AOP统一异常处理 简介 在Controller层,Service层,可能会有很多的try catch代码块.这将会严重影响代码的可读性."美观性".怎样才可以把更多 ...

  7. linux下载安装常用的配置,jdk,mysql,tomcat,redis

    1.特别强调,本教程适合于VMware Workstation创建的虚拟机linux配置. 2.ssh---linux连接的工具 https://pan.baidu.com/s/1MGIr5WOkkH ...

  8. Linux——CentOS7没有第二张网卡的配置信息

    前言 为了一个实验做测试,在VMware中配置了环境,但是配置了双网卡后发现第二张网卡没有配置文件. 都是些基本命令就不写了,图里也有. 系统 : CentOS7.6 步骤 查看网卡信息 使用ip a ...

  9. Xms Xmx PermSize MaxPermSize的含义

    参数的含义 -vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M -vmargs 说明后面是VM的参数,所以后面的其实都是JV ...

  10. python面试题&练习题之运算符与if控制

    1.任意的输入10个数字,按从大到小排序 l2 = [] for i in range(1,11): num = input('输入第{}个数字'.format(i)) if num.isdigit( ...