454. 4Sum II

Add to List

Description Submission Solutions

  • Total Accepted: 8398
  • Total Submissions: 18801
  • Difficulty: Medium
  • Contributors: Samuri

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

Subscribe to see which companies asked this question.

【题目分析】

给定四个长度相同的数组,在每个数组中取一个数字,在所有的组合中和为零的组合有多少个?

【思路】

把四个数组分为两组,每组包含两个数组。把其中一组中的任意两个值和存入hashmap中,然后在hashmap查找另外两个数组的值的组合。这其实是相当于转化为了一个two sum问题。

【java代码】

 public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
Map<Integer, Integer> map = new HashMap<>(); for(int i=0; i<C.length; i++) {
for(int j=0; j<D.length; j++) {
int sum = C[i] + D[j];
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
} int res=0;
for(int i=0; i<A.length; i++) {
for(int j=0; j<B.length; j++) {
res += map.getOrDefault(-1 * (A[i]+B[j]), 0);
}
} return res;
}

代码中的map.getOrDefault(sum, 0)相比先在map中查找再取数的操作是比较高效的。

LeetCode 454. 4Sum II的更多相关文章

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

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

  3. LeetCode 454. 4Sum II (C++)

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

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

  5. LC 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】454. 4Sum II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  7. 【LeetCode】454 4Sum II

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

  8. 1. Two Sum + 15. 3 Sum + 16. 3 Sum Closest + 18. 4Sum + 167. Two Sum II - Input array is sorted + 454. 4Sum II + 653. Two Sum IV - Input is a BST

    ▶ 问题:给定一个数组 nums 及一个目标值 target,求数组中是否存在 n 项的和恰好等于目标值 ▶ 第 1题,n = 2,要求返回解 ● 代码,160 ms,穷举法,时间复杂度 O(n2), ...

  9. 454 4Sum II 四数相加 II

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

随机推荐

  1. PAT 1077 Kuchiguse [一般]

    1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Personal ...

  2. 如何下载java的jdk

    1.https://www.oracle.com/index.html 2.https://www.oracle.com/downloads/index.html 3.https://www.orac ...

  3. idea新建的项目,文件夹右键不能新建class

    一般情况下,新建的mave项目,通常没有XXX\src\main\java这个目录,如果手动创建,则又不能右键build与java相关的,强行建立的话,也不会被idea所识别,更不会被虚拟机编译执行. ...

  4. dubbo总结

     一 .Dubbo产生背景 单一应用架构当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本.此时,用于简化增删改查工作量的 数据访问框架(ORM) 是关键. 垂直应用架构当访 ...

  5. $python collections库

    collections库是python内置的集合库,本文主要讲解以下5种数据结构的用法: namedtuple 命名元组,是tuple的子类 deque 双向列表 defaultdict 有默认值的字 ...

  6. [日志]logback告警

    开发过程中,难免会有发生错误或异常的时候,有些是需要及时通知到相关开发人员的.logback可以通过简单的配置达到邮件告警的目的. 一.错误告警 如下配置,所有Error级别的log发送邮件告警给re ...

  7. DNS ARP地址解析原理

    ARP是地址解析协议 主机A与主机B之间如果要进行数据间的传输,需要获取对方的IP与物理地址(MAC),在只清楚ip的情况下,主机A向主机B请求链接,会先查找ARP高速缓存表,是否存在对应的主机B的i ...

  8. 20145230熊佳炜《网络对抗》实验九:web安全基础实践

    20145230熊佳炜<网络对抗>实验九:web安全基础实践 webgoat webgoat的中文是代罪羔羊的意思,而它是一个有很多漏洞的web应用程序,我们可以利用它来研究关于web应用 ...

  9. 输入n,求1~n累加

    最开始可能会使用for循环来计算,现在直接使用等差数据计算和公式:s=(a0+n)*n/2 long sum(int n) { long ret=0: ret = (1+n)* n /2: retur ...

  10. MR案例:WordCount改写

    请参照wordcount实现一个自己的MapReduce,需求为:     a. 输入文件格式:        xxx,xxx,xxx,xxx,xxx,xxx,xxx     b. 输出文件格式:   ...