leetcode454
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的更多相关文章
- 【算法训练营day7】LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和
[算法训练营day7]LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和 LeetCode454. 四数相加I ...
- [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 ...
- LeetCode454. 四数相加 II
题目 给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0. 分析 关键是如何想到用 ...
- 【哈希表】leetcode454——四数相加II
编号454:四数相加II 给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0. 为 ...
- LeetCode通关:哈希表六连,这个还真有点简单
精品刷题路线参考: https://github.com/youngyangyang04/leetcode-master https://github.com/chefyuan/algorithm-b ...
随机推荐
- 【scala】数组和列表
一.数组Array 1.创建数组 隐式:val greetStrings = new Array[String](3); 显式:val greetStrings : Array[String] = n ...
- LeetCode OJ:Pow(x, n) (幂运算)
Implement pow(x, n). 幂运算,简单的方法snag然很好实现,直接循环相乘就可以了,但是这里应该不是那种那么简单,我的做法使用到了一点递归: class Solution { pub ...
- css3 hover 的一些小效果
Hover 2D Transforms Grow Shrink Pulse Pulse-grow Pulse-shrink Push Top Rotate Grow-rotate Float Sink ...
- 面试题总结(三)、《STL源码剖析》相关面试题总结
声明:本文主要探讨与STL实现相关的面试题,主要参考侯捷的<STL源码剖析>,每一个知识点讨论力求简洁,便于记忆,但讨论深度有限,如要深入研究可点击参考链接,希望对正在找工作的同学有点帮助 ...
- json 和 table控件
<!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content= ...
- linux中文件或者文件夹的基本操作(复制,移动,删除,查找,压缩)
linux 文件(文件夹)的创建,复制,移动,重命名,删除基本命令 复制文件或整个目录 cp 源文件名 目标文件夹/[目标文件名]cp -rv 源文件夹 目标文件夹/[目标文件夹名] --r 递归目录 ...
- HDU - 5289:Assignment(单调队列||二分+RMQ||二分+线段树)
Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this com ...
- Elasticsearch.net项目
Elasticsearch.net项目实战 https://www.cnblogs.com/lucky_hu/p/9746736.html elasticsearch.net项目实战 @智客幸达 目录 ...
- Bootstrap-table学习笔记(二)——前后端分页模糊查询
在使用过程中,一边看文档一边做,遇到了一些困难的地方,在此记录一下,顺便做个总结: 1,前端分页 2,后端分页 3,模糊查询 前端分页相当简单,在我添加了2w条测试数据的时候打开的很流畅,没有卡顿. ...
- 十八、python沉淀之路--生成器
一.生成器 生成器总结:语法上和函数类似:生成器函数和常规函数几乎是一样的.他们都是使用def语句进行定义,差别在于生成器使用yield语句返回一个值,而常规函数使用return语句返回一个值.自动实 ...