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

能够优化到O(n2),但是可以只用一个map,如下,我本来的想法是用两个map,但这样就浪费了空间。

Runtime: 155 ms, faster than 67.41% of Java online submissions for 4Sum II.

class Solution {
private Map<Integer, Integer> mp1 = new HashMap<>();
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
for(int i = ; i<A.length; i++){
for(int j=; j<B.length; j++){
mp1.put(A[i] + B[j], mp1.getOrDefault(A[i]+B[j],) + );
}
}
int ret = ;
for(int i = ; i<C.length; i++){
for(int j=; j<D.length; j++){
ret += mp1.getOrDefault(-*(C[i]+D[j]), );
}
}
return ret;
}
}

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

  1. LeetCode 454. 4Sum II

    454. 4Sum II Add to List Description Submission Solutions Total Accepted: 8398 Total Submissions: 18 ...

  2. 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. 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), ...

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

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

  6. 454. 4Sum II ——查找本质:hash最快,二分次之

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

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

  8. 【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 ...

  9. 454 4Sum II 四数相加 II

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

随机推荐

  1. java程序启动极慢的问题处理

    在程序部署过程中,遇到一次java程序启动极慢的情况 参考:https://www.iteye.com/blog/windshome-1836885 原部署环境是有外网的,启动java极快 后来极其修 ...

  2. 【SpringBoot】整合日志框架

    一.日志框架概述 1.1 日志框架的产生 1.2 市面上的日志框架 二.SLF4j 使用与整合 2.1 如何在系统中使用SLF4j 2.2 如何整合日志框架 2.3 SpringBoot中的日志关系 ...

  3. 第五章、Django之多表查询进阶与事务

    目录 第五章.Django之多表查询 一.聚合查询 二.分组查询 三.F与Q查询 四.查询优化 五.Django开启事务 六.自定义char字段 七.ORM常用字段 第五章.Django之多表查询 一 ...

  4. 【Java基础 项目实例--Bank项目5】Account 和 customer 对象等 继承、多态、方法的重写

    延续 Java基础 项目实例--Bank项目4 实验要求 实验题目 5: 在银行项目中创建 Account 的两个子类:SavingAccount 和 CheckingAccount 实验目的: 继承 ...

  5. VMware厚置备延迟置零,厚置备置零,精简置备详解

    1.厚置备延迟置零(zeroed thick) 以默认的厚格式创建虚拟磁盘.创建过程中为虚拟磁盘分配所需空间.创建时不会擦除物理设备上保留的任何数据,但是以后从虚拟机首次执行写操作时会按需要将其置零. ...

  6. string::cbegin string::cend

    const_iterator cbegin() const noexcept; const_iterator cend() const noexcept;注:返回常量迭代器,不能修改 #include ...

  7. 数据统计,包括mysql和MongoDB

    select ct.dt, COUNT(DISTINCT c.id) from tms_service_customer c, tms_dispatch_details d, (select DIST ...

  8. RHEL8 创建本地YUM存储库

    yum 的好处及本地yum的好处不在本文讨论范畴,本文针对rhel8中的新功能yum做简要介绍和配置,在 RHEL 8中分为两个存储库: BaseOS 应用程序流(AppStream) BaseOS中 ...

  9. php操作kafka

    php操作kafka----可以参照网上的安装步骤,先安装ldkafka rdkafka,然乎启动zookeeper和kafka服务器 <?php //$conf = new Rdkafka\P ...

  10. Peter Shirley-Ray Tracing The Next Week

    Peter Shirley-Ray Tracing The Next Week(2016) 原著:Peter Shirley 英文原著地址 密码: urji 第二本书主要介绍了运动模糊,BVH(层次包 ...