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 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的更多相关文章
- LeetCode 454. 4Sum II
454. 4Sum II Add to List Description Submission Solutions Total Accepted: 8398 Total Submissions: 18 ...
- 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 ...
- 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), ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- 【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 ...
- 454 4Sum II 四数相加 II
给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0.为了使问题简单化,所有的 A, ...
随机推荐
- Java虚拟机内存基础、垃圾收集算法及JVM优化
1 JVM 简单结构图 1.1 类加载子系统与方法区 类加载子系统负责从文件系统或者网络中加载 Class 信息,加载的类信息存放于一块称 为方法区的内存空间.除了类的信息外,方法区中可能还会存放 ...
- php连接docker运行的mysql,显示(HY000/2002): Connection refused的解决办法
php要连接docker中运行的mysql是不能用localhost, 127.0.0.1来连接的,因为每个docker运行容器的localhost 127.0.0.1都是自己容器本身,不是mysql ...
- Vue介绍:vue项目搭建
一.环境搭建 二.项目创建 三.认识项目 四.项目功能 一.环境搭建 *安装node 官网下载安装包,傻瓜式安装:https://nodejs.org/zh-cn/ *安装cnpm npm insta ...
- redis + boost.asio
redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...
- tsung压力测试环境部署详细步骤(内附安装包)
操作系统: Redhat 6.3.Redhat6.5 .centos7.4(这些版本已验证过) tsung版本: tsung-1.6.0 下载地址: 链接: https://pan.baidu.com ...
- zabbix验证微信
在Zabbix服务端设置邮件报警,当被监控主机宕机或者达到触发器预设值时,会自动发送报警邮件到指定邮箱. 具体操作: 以下操作在Zabbix监控服务端进行 备注:Zabbix监控服务端 操作系统:Ce ...
- Linux (Deppin ,Ubuntu )开发环境配置,VUE & dotnetcore 解决 yarn 找不到问题
新装系统设置 清华镜像: https://mirror.tuna.tsinghua.edu.cn/help/ubuntu/ 网易镜像: http://mirrors.163.com/.help/ubu ...
- new一个对象的过程
不用死记硬背,理解才是硬道理.只需要写个例子,然后输出看一下就清楚了 首先我们看下new Person输出什么? var Person = function(name, age) { this.nam ...
- SpringBoot集成Druid实现监控
application.properties文件完整信息 #连接数据库 spring.datasource.driver-class-name=org.mariadb.jdbc.Driver spri ...
- python下vs的使用
part 1:导入pygame包 在python环境下:视图---其他窗口--python环境,选择从pypi安装pygame