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. SpringBoot启动流程及其原理

    Spring Boot.Spring MVC 和 Spring 有什么区别? 分别描述各自的特征: Spring 框架就像一个家族,有众多衍生产品例如 boot.security.jpa等等:但他们的 ...

  2. springboot+mybatisplus进行整合并且使用逆向工程

    首先引入maven依赖:这是整合mybatisplus时,进行逆向工程时候需要引入的依赖 <!--mybaitsplus start--> <dependency> <g ...

  3. easyui_验证扩展

    本文为转载,并非原创 easyui validatebox 验证类型 分类: jquery-easyUI -- : 11000人阅读 评论() 收藏 举报 easyuiValidateBox requ ...

  4. Java线程池(ExecutorService)使用

    一.前提 /** * 线程运行demo,运行时打出线程id以及传入线程中参数 */ public class ThreadRunner implements Runnable { private fi ...

  5. junit4的初级用法

    junit4初级用法: 一:各个标签的意思 1.@Test用来标注测试函数 2.@Before用来标注此函数在每次测试函数运行之前运行(每执行一个@Test之前都要运行一遍@Before) 3.@Af ...

  6. codeforces1276A As Simple as One and Two

    C.As Simple as One and Two A. As Simple as One and Two time limit per test 3 seconds memory limit pe ...

  7. learning java 文件锁

    import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; i ...

  8. mov offset和lea的区别

    mov offset和lea的区别  原文地址:https://www.cnblogs.com/fanzi2009/archive/2011/11/29/2267725.html 全局变量取地址用mo ...

  9. jQuery - 添加元素append/prepend和after/before的区别

    append <p> <span class="s1">s1</span> </p> <script> $(" ...

  10. [转]查看 docker 容器使用的资源

    作者:sparkdev 出处:http://www.cnblogs.com/sparkdev/     在容器的使用过程中,如果能及时的掌握容器使用的系统资源,无论对开发还是运维工作都是非常有益的.幸 ...