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
class Solution(object):
def fourSumCount(self, A, B, C, D):
"""
:type A: List[int]
:type B: List[int]
:type C: List[int]
:type D: List[int]
:rtype: int
Input:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]
Output:
2
A[i]+B[j]=E[n*n] [-1,0,0,1]
C[i]+D[j]=F[n*n] [-1,1,2,4]
E[i]+E[j]=0 -1+1=0, 1+(-1)=0
"""
E = {}
for a in A:
for b in B:
if a+b not in E:
E[a+b] = 0
E[a+b] += 1
ans = 0
for c in C:
for d in D:
if -(c+d) in E:
ans += E[-(c+d)]
return ans

454. 4Sum II ——查找本质:hash最快,二分次之的更多相关文章

  1. LeetCode 454. 4Sum II

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

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

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

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

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

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

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

  9. [刷题] 454 4Sum II

    要求 给出四个整型数组ABCD,寻找有多少 i j k l 的组合,使得A[i]+B[j]+C[k]+D[l]=0 ABCD元素个数均为N,0<=N<=500 示例 输入: A = [ 1 ...

随机推荐

  1. hdu 2112 (最短路+map)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others)  ...

  2. Codeforces Round #257 (Div. 2) A题

    A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes input standar ...

  3. Apache-Shiro+Zookeeper系统集群安全解决方案之会话管理

    如今的系统多不是孤军奋战,在多结点会话共享管理方面有着各自的解决办法,比如Session粘连,基于Web容器的各种处理等或者类似本文说的完全接管Web容器的Session管理,只是做法不尽相同. 而本 ...

  4. apecceosummit2016

    https://www.apecceosummit2016.com/program.html Thursday 17 November 2016 9:00am - 9:00pm REGISTRATIO ...

  5. spring构造函数注入、setter方法注入和接口注入

    Spring开发指南中所说的三种注入方式: Type1 接口注入 我们常常借助接口来将调用者与实现者分离.如: public class ClassA { private InterfaceB clz ...

  6. js输出26个字母两种方法(js fromCharCode的使用)

    方法一 var character = new Array("A","B","C","D","E", ...

  7. BigTale

    Google's BigTable 原理 (翻译)     题记:google 的成功除了一个个出色的创意外,还因为有 Jeff Dean 这样的软件架构天才.                     ...

  8. Java Date,long,String 日期转换

    1.java.util.Date类型转换成long类型java.util.Date dt = new Date();System.out.println(dt.toString()); //java. ...

  9. Yii2.0 依赖注入(DI)和依赖注入容器的原理

    依赖注入和依赖注入容器 为了降低代码耦合程度,提高项目的可维护性,Yii采用多许多当下最流行又相对成熟的设计模式,包括了依赖注入(Denpdency Injection, DI)和服务定位器(Serv ...

  10. RTB

    RTB —— Real Time Bidding 的简称,就是实时竞价.跟传统购买形式相比,RTB是在每一个广告展示曝光的基础上进行竞价,就是每一个PV都会进行一次展现竞价,谁出价高,谁的广告就会被这 ...