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. PHP中RabbitMQ之phpAmqplib实现(五

    本章讲诉如何使用php-amqplib实现RabbitMQ. 环境:CoentOS,PHP 7 简单介绍一下php-amqplib php-amqplib是Advanced Message Queui ...

  2. Celery 初步使用心得

    一. 基本介绍 Celery是一个专注于实时处理和任务调度的分布式任务队列.所谓任务就是消息,消息中的有效载荷中包含要执行任务需要的全部数据. 使用Celery常见场景: Web应用.当用户触发的一个 ...

  3. Oracle笔记(十二) 集合、序列

    一.集合 在数学的操作之中存在交.差.并.补的概念,而在数据的查询中也存在此概念,有如下几个连接符号: UNION:连接两个查询,相同的部分不显示: UNION ALL:连接两个查询,相同的部分显示: ...

  4. eclipse设置打开java文件目录

    1. 第一步: 2. 第二步: 3. 第三步: Location:C:/WINDOWS/explorer.exe Arguments:${container_loc}

  5. 关于IO的同步,异步,阻塞,非阻塞

    上次写了一篇文章:Unix IO 模型学习.恰巧在这次周会的时候,@fp1203 (goldendoc成员之一) 正好在讲解poll和epoll的底层实现.中途正好讨论了网络IO的同步.异步.阻塞.非 ...

  6. js过滤时间格式

    Date.prototype.Format = function(fmt) { //author: meizz var o = { "M+" : this.getMonth()+1 ...

  7. [唐胡璐]Selenium技巧- 抓图并保存到TestNG报告中

    这里不讲解怎么在Eclipse安装配置TestNG,网上一搜一大把,大家自己去实践一下。 在这里主要说一下用Java来实现Selenium Webdriver的截图功能和把截图写到TestNG的报告中 ...

  8. Centos6.5 下安装配置Apache+PHP+Mysql环境

    1.准备工作 # yum -y update && yum -y install vim make cmake gcc gcc-c++ bison bison-devel ncurse ...

  9. 前端小白页面开发注意事项及小工具(html\css\js)

    技术一直在向前发展.但是有一些是相通的,要找准重点,将80%的时间放在提升基础问题上,余下的20%再去学习框架,库和工具. HTML 1. HTML 属性应当按照以下给出的顺序依次排列,确保代码的易读 ...

  10. 第40题:组合总和II

    一.问题描述: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合 ...