public class Solution {
public int FourSumCount(int[] A, int[] B, int[] C, int[] D) {
var dic = new Dictionary<int, int>(); for (int i = ; i < C.Length; i++)
{
for (int j = ; j < D.Length; j++)
{
int sum = C[i] + D[j];
if (!dic.ContainsKey(sum))
{
dic.Add(sum, );
}
else
{
dic[sum]++;
}
}
} int res = ;
for (int i = ; i < A.Length; i++)
{
for (int j = ; j < B.Length; j++)
{ var cur = ;
var oppo = - * (A[i] + B[j]);
if (dic.ContainsKey(oppo))
{
cur = dic[oppo];
}
res += cur;
}
} return res;
}
}

https://leetcode.com/problems/4sum-ii/#/description

补充一个python的版本:

 class Solution:
def fourSumCount(self, A: 'List[int]', B: 'List[int]', C: 'List[int]', D: 'List[int]') -> int:
partone = {}
res =
for a in A:
for b in B:
cur = a + b
if cur in partone:
partone[cur] +=
else:
partone[cur] = for c in C:
for d in D:
cur = c + d
if -cur in partone:
res += partone[-cur] return res

leetcode454的更多相关文章

  1. 【算法训练营day7】LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和

    [算法训练营day7]LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和 LeetCode454. 四数相加I ...

  2. [Swift]LeetCode454. 四数相加 II | 4Sum II

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l)there are such th ...

  3. LeetCode454. 四数相加 II

    题目 给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0. 分析 关键是如何想到用 ...

  4. 【哈希表】leetcode454——四数相加II

    编号454:四数相加II 给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0. 为 ...

  5. LeetCode通关:哈希表六连,这个还真有点简单

    精品刷题路线参考: https://github.com/youngyangyang04/leetcode-master https://github.com/chefyuan/algorithm-b ...

随机推荐

  1. mysql 习题

    习题1 1.创建一个库(dt55_mysql),在库下创建一张students表 (1)students表中的字段有:id(int类型),stuName(varchar类型),age(int类型),w ...

  2. 【Java】访问权限

    一.访问权限修饰词 关键字 名称 本类 同一包中的类 子类 其他包中的类 public  接口访问权限 √ √ √ √ protected 继承访问权限 √ √ √ x 默认 包访问权限  √ √ x ...

  3. git合并分支与解决冲突

    前提: 当前开发的分支为feature/20161129_317606_algoplatform_1,由于feature/20161130_322574_tmstools_1分支有新内容,所以准备将f ...

  4. Jedis操作Redis技巧详解

    对于Redis的部署模式有两种,单机模式 和 集群模式.因此,本文的介绍也从这两个方面进行介绍.众所周知,Jedis是最著名的Redis java客户端操作类库,几乎支持所有的Redis操作.本文就是 ...

  5. Leetcode 1005. Maximize Sum Of Array After K Negations

    class Solution(object): def largestSumAfterKNegations(self, A, K): """ :type A: List[ ...

  6. RedHat Server Enterprise 6安装G++

    RedHat 6默认是安装有GCC,而没有安装G++编译 要安装G++前最好先查看下GCC的版本号,通常GCC的版本和G++的版本是相同的,知道GCC的版本再去找G++的安装文件就容易些,版本号有在安 ...

  7. bzoj 3173 最长上升子序列

    Written with StackEdit. Description 给定一个序列,初始为空.现在我们将\(1\)到\(N\)的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字, ...

  8. proxifier 注册码 +电脑全局代理设置

    L6Z8A-XY2J4-BTZ3P-ZZ7DF-A2Q9C(Portable Edition)  5EZ8G-C3WL5-B56YG-SCXM9-6QZAP(Standard Edition)  P4 ...

  9. WPF简单模拟QQ登录背景动画(转)

    介绍 之所以说是简单模拟,是因为我不知道QQ登录背景动画是怎么实现的.这里是通过一些办法把它简化了,做成了类似的效果 效果图 大体思路 首先把背景看成是一个4行8列的点的阵距,X轴Y轴都是距离70.把 ...

  10. bzoj 3998 [TJOI2015]弦论——后缀自动机

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3998 相同子串算多个的话,先求好 right ,然后求一个 sm 表示走到这个点之后有几种走 ...