https://leetcode.com/submissions/detail/153740275/

class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int,int> CD_freq;
for (auto i : C)
for (auto j : D)
CD_freq[i+j]++; int res = ;
for (auto a : A)
for (auto b : B) {
auto it = CD_freq.find(-(a+b));
if (it != CD_freq.end())
res += it->second;
}
return res;
}
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int,int> CD_freq;
for (auto i : C)
for (auto j : D)
CD_freq[i+j]++; int res = ;
for (auto a : A)
for (auto b : B)
res += CD_freq[-(a + b)];  // If CD_freq[-(a+b)] doesn't exist, it will be added! So map becomes much bigger!!!
return res;
}
int fourSumCount_n3(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int,int> D_freq;
for (auto i : D)
D_freq[i]++; int res = ;
for (int i = ; i < A.size(); i++)
for (int j = ; j < B.size(); j++)
for (int k = ; k < C.size(); k++)
res += D_freq[-(A[i] + B[j] + C[k])];
return res;
}
int fourSumCount_n4(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
int res = ;
for (int i = ; i < A.size(); i++)
for (int j = ; j < B.size(); j++)
for (int k = ; k < C.size(); k++)
for (int l = ; l < D.size(); l++)
if (A[i] + B[j] + C[k] + D[l] == )
res++;
return res;
}
};

4Sum II的更多相关文章

  1. LeetCode 454. 4Sum II

    454. 4Sum II Add to List Description Submission Solutions Total Accepted: 8398 Total Submissions: 18 ...

  2. 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 ...

  3. 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 ...

  4. [LeetCode] 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. [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 ...

  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: 4Sum II

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

  8. 454. 4Sum II ——查找本质:hash最快,二分次之

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

  9. [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 ...

  10. 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), ...

随机推荐

  1. Java 内存模型(零)

    经典老图: 方法区和堆:由所有线程共享的数据区 虚拟机栈和本地方法栈和程序计数器:线程隔离的数据区 后面一步步讲这个图里面所涉及到的内容,待续...

  2. Soup协议-即普通post请求,内容域xml

    1.基础问题 1.1 soup-Simple Object Access Protocal简单对象访问协议 a).承载在http协议之上,http支持传输img/html/文件等,soup请求和响应域 ...

  3. xenserver 更新源

    在xenserver上安装vnc软件时,报错 [root@cloud yum-3.4.3]# ./yummain.py install yumThere are no enabled repos.Ru ...

  4. 如何远程连接非默认端口SQL Server

    SQL Server Management Studio建立远程SQL连接  连接的时候写: 127.0.0.1,49685\sqlexpress 记得使用逗号,不是冒号

  5. MVVM技术 - 的实现 @{}来进行 调用那个 DataBinding方法

    new Material Design 支持哭 还有 Data Binding 结束   使用DataBindign 结束 我们很方面的实现 MVVM设计模式   什么是MVVM model 呢.   ...

  6. POS开发问题 - 跳转页面更新,返回还原旧数据

    问题描述:由于需求的需要,路由需要加上缓存 <keep-alive> ,还要实现跳转就初始化,返回就还原的需求.意思就是:页面 A 跳转到页面 B ,页面 B 要初始化数据,但是 页面 B ...

  7. Java集合框架—List

    Collection |--List:元素是有序的,元素可以重复.因为该集合体系有索引. |--ArrayList:底层的数据结构使用的是数组结构.特点:查询速度很快.但是增删稍慢.线程不同步. |- ...

  8. Eucalyptus-利用镜像启动一个Windows Server 2008r2实例

    1.前言 使用kvm制作Eucalyptus镜像(Windows Server 2008r2为例)——http://www.cnblogs.com/gis-luq/p/3990792.html 上一篇 ...

  9. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:11.定制化Log输出

    欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 前言 在<迷你微信>服务器中,我们用了Log4J来进行输出,这可以在我们程序出现异常的时候找到错误发生时 ...

  10. Centos7_Minimal-1611 版安装python3.5.3

    前提 最近在学习python3,看到好多教程都是要求在Windows或者Ubuntu 平台上使用,安装比较方便.由于不在想Winddows上安装也没有Ubutnu系统 ,所以在自己的CentOS7上面 ...