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. Socket通信常用方法

    使用tcp协议,链接服务器的方法 /// <summary> /// 连接使用tcp协议的服务端 /// </summary> /// <param name=" ...

  2. 第二篇,MVC框架

    MVC : 模型(model)---视图(view)---控制器(caontreller)的缩写 MVC是一个设计模式,他强制性的使应用程序的输入,处理,输出 分开 模型(Model):程序员编写程序 ...

  3. 将图片转成base64 小工具

    工作需要使用,所以就做了一个小工具,方便使用 推荐使用 chrome,ff . 毕竟是个小工具方便自己使用而已,所以没有做浏览器兼容测试了! 代码如下,直接保存为 .html 打开即可 <!DO ...

  4. 键盘ctrl+shift不能切换输入法

    1.首先检查输入法是否被卸载,如没被卸载: 2.使用下面如图方法进行设置

  5. iOS - OC NSArray 数组

    前言 @interface NSArray<__covariant ObjectType> : NSObject <NSCopying, NSMutableCopying, NSSe ...

  6. EF中使用SQL语句或存储过程(小笔记)

    1.无参数查询 var model = db.Database.SqlQuery<UserInfo>("select* from UserInfoes ").ToLis ...

  7. mysql批量替换数据库某字段部分内容

    update 表名 set 字段名=replace(字段名,’要替换的内容’,’替换后的内容’) eg:修改scenario表中的picture字段中的ip地址. UPDATE scenario SE ...

  8. Python学习(18)面向对象

    目录 Python 面向对象 创建实例对象 Python内置类属性 Pyyhon对象销毁(垃圾回收) 类属性与方法 Python 面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此 ...

  9. android下基本json串的生成与解析

    以前就用过json串,不过是在java环境下面,如今转移到android环境下,java里面生成解析json串的jar包与android中自带的冲突,所以也只能用安卓自带的.   先前查网上的资料,感 ...

  10. SDL1.2到2.0的迁移指南(转)

    里面有些单词不好翻译所以放在开头,以备查验. BLock Image Transfer, a computer graphics operation in which two bitmap patte ...