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. DOM解析XML文件例子

    DOM解析XML文件是一次性将目标文件中的所有节点都读入,然后再进行后续操作的方式. 一般分为以下几步: 1. 定义好目标XML文件路径path . 2. 实例化DOM解析工厂对象 ,Document ...

  2. .NET学习路线之我见(转)

    这是我的第一篇博客,所以,我想写个大的,至少这个话题是比较大的. 在文章的开头,首先声明,这篇文章仅代表我个人的想法,并且只适合.NET的初学者,如果你已经有两年以上的开发经验,我劝你还是别看了,省得 ...

  3. std::hash实现太简单分布不匀

    std::hash实现太简单分布不匀(金庆的专栏 2017.5)#include <iostream>#include <functional>using namespace ...

  4. 2017年国内已经开设机器人工程专业(080803T)高校名单

    相关资料来源于教育部公布的2014年度和2016年度普通高等院校本科专业备案或审批结果的通知: 2014年批次 http://www.moe.edu.cn/publicfiles/business/h ...

  5. Angular提示文件不是一个有效的模块

    ERROR in src/app/app.component.ts(2,23): error TS2306: File '/home/jerry/angular/todo/src/app/model. ...

  6. Python 安装 pip package

    Python的 package 站点提供的msi安装越来越少了,如今大多是.whl或.tar.gz格式.对某些用windows的小白(比如,我)来说,对.tar.gz闻所未闻,也纠结了很长时间.whl ...

  7. HDU - 6231:K-th Number (不错的二分)

    Alice are given an array A[1..N]A[1..N] with NN numbers. Now Alice want to build an array BB by a pa ...

  8. bzoj 4753 最佳团体

    Written with StackEdit. Description \(JSOI\)信息学代表队一共有N名候选人,这些候选人从\(1\)到\(N\)编号.方便起见,\(JYY\)的编号是\(0\) ...

  9. codechef Graph on a Table

    codechef Graph on a Table https://www.codechef.com/problems/TBGRAPH 题意 : 一个\(n\times m\)的网格图.\(q\) 个 ...

  10. LeetCode Kill Process

    原题链接在这里:https://leetcode.com/problems/kill-process/description/ 题目: Given n processes, each process ...