Leetcode: 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(n^3), space O(N)的做法,后来发现还可以优化
Solution: time O(N^2), space O(N^2)
public class Solution {
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i=0; i<A.length; i++) {
for (int j=0; j<B.length; j++) {
map.put(A[i]+B[j], map.getOrDefault(A[i]+B[j], 0) + 1);
}
}
for (int i=0; i<C.length; i++) {
for (int j=0; j<D.length; j++) {
res += map.getOrDefault(-(C[i]+D[j]), 0);
}
}
return res;
}
}
Leetcode: 4Sum II的更多相关文章
- [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 ...
- LeetCode 454. 4Sum II
454. 4Sum II Add to List Description Submission Solutions Total Accepted: 8398 Total Submissions: 18 ...
- [LeetCode] 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 ...
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- LeetCode——4Sum & 总结
LeetCode--4Sum & 总结 前言 有人对 Leetcode 上 2Sum,3Sum,4Sum,K Sum问题作了总结: http://blog.csdn.net/nanjunxia ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- java enum(枚举)使用详解 + 总结
enum 的全称为 enumeration, 是 JDK 1.5 中引入的新特性,存放在 java.lang 包中. 下面是我在使用 enum 过程中的一些经验和总结,主要包括如下内容: 1. 原始 ...
- Hibernate学习笔记1
xml文件[封装]写SQL语句<class class='com.briup.user',table='s_emp'> <property name='id' column='id' ...
- C#常用方法集合
public class Utility:Page { #region 数据转换 /// <summary> /// 返回对象obj的String值,obj为null时返回空值. /// ...
- 【MongoDB】2014-07-25T11:00:48.634+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errno:10061 由于目标计算机积极拒绝,无法连接。
1:启动MongoDB 2014-07-25T11:00:48.634+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errn ...
- jquery插件之拖拽改变元素大小
该插件乃本博客作者所写,目的在于提升作者的js能力,也给一些js菜鸟在使用插件时提供一些便利,老鸟就悠然地飞过吧. 此插件旨在实现目前较为流行的拖拽改变元素大小的效果,您可以根据自己的实际需求来设置被 ...
- 模拟ATM机银行系统
淄博汉企Java基础考核项目 模拟银行自助终端系统 一. 本系统模拟银行用户使用ATM机开户.查询.存款.取款功能,要求使用java语言编程实现. 说明: 1. 对于数据输入异常,可使用java异常处 ...
- Oracle--数据库中的五种约束
数据库中的五种约束 数据库中的五种约束及其添加方法 五大约束 1.--主键约束(Primay Key Coustraint) 唯一性,非空性 2.--唯一约束 (Unique Counstraint ...
- 创建catalog数据库
1数据库版本 2环境准备 3目录数据库准备 4创建目录数据库 5使用目录数据库
- Oracle数据库表复制语句
Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) values(value1,value2,...)这种形式的在应用程序开发中必不可少.但我 ...
- zju(3)内核编译与运行
1.实验目的 学习和掌握Linux配置和编译的基本步骤. 二.实验内容 1. 对Linux内核及用户程序进行配置: 2. 编译生成内核映像文件: 3. 把编译的映像文件烧写到FLASH中,查看运行结果 ...