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. LeetCode OJ:Product of Array Except Self(除己之外的元素乘积)

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...

  2. CSS:Tutorial three

    1.CSS Links links can be styled differently depending on what state they are in. The four links stat ...

  3. LEX下出毛病的问题

    毛病! 1.今日写词法分析,回想起第一次写时候的蓝色警告:不要随便管理员.so,便Win+R,"cmd",回车. 2.在用lex写的时候,注意注释是 /*注释放于此处*/ 而非一般 ...

  4. [python] 获得所有的最长公共子序列

    两句闲话 得到两个序列的最长公共子序列(LCS)是个经典问题,使用动态规划,实现起来并不难. 一般来说,我们只是输出一个LCS.但是,老师布置的作业是输出所有的LCS. 解法 按照一般的方法,我们首先 ...

  5. docx转doc时,防止公式被转成图片的解决办法

    编辑社回复需要doc(Word 97-2003)格式的文档,可是将docx(Word 2007+)另存为doc格式时,发现公式被转成了图片.其实,最简单的办法就是,打个电话过去给编辑社:“大爷,拜托您 ...

  6. C++之内存管理

    内存管理是C++最令人切齿痛恨的问题,也是C++最有争议的问题,C++高手从中获得了更好的性能,更大的自由,C++菜鸟的收获则是一遍一遍的检查代码和对C++的痛恨,但内存管理在C++中无处不在,内存泄 ...

  7. 有返回值的多线程demo

    package com.jimmy.demo.util; import java.util.HashMap;import java.util.concurrent.*;import java.util ...

  8. checkStype和findBugs校验

    IDEA可以直接在setting中下载checkStyle和findBugs <plugin> <groupId>org.codehaus.mojo</groupId&g ...

  9. Kubernetes安装部署演示介绍

    四.安装k8s 1.安装 使用的是k8s 1.2.4版本. 将kubernetes.tar.gz 上传主机,并解压. tar -xzvf kubernetes.tar.gz cd kubernetes ...

  10. COGS 2259 异化多肽——生成函数+多项式求逆

    题目:http://cogs.pro:8080/cogs/problem/problem.php?pid=2259 详见:https://www.cnblogs.com/Zinn/p/10054569 ...