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. Access 将SQL查询结果强制转换为某种类型

    每个函数都可以强制将一个表达式转换成某种特定数据类型. 语法 CBool(expression) CByte(expression) CCur(expression) CDate(expression ...

  2. vue中background-image图片路径问题

    按照以往在css文件中写background:url('图片路径'),完成后加载竟然显示出错,起初以为路径不对,检查了几遍,仍然没有问题.最后百度找答案,发现不少同行都遇到过这种问题,遂记录下自己所采 ...

  3. Git命令--保存用户名和密码

    使用git各项操作时,总是会出现输入密码的弹窗,且需要多次输入,很是繁琐,通过git命令可以记住密码,避免多次操作. 一.创建保存密码的文件 1.在home文件夹,一般是 C:\Documents a ...

  4. 创见VR-上海,会后总结

    第一次,参加这种VR会,感觉不错.上午突然发现自己之前的一款AR Demo下载量在10-50了,真没想到,虽然这款Demo有一处bug至今未修复 ^^.不过,看来现在AR/VR确实恨火. ZSpace ...

  5. 【Oracle】曾经的Oracle学习笔记(1-3) 数据库常见用语,常见命令,创建测试表

    一.数据库的登录 二.数据库常用语 三.测试表的创建,测试数据初始化 四.常见命令介绍 五.测试 user:jeffreysn:jeffrey user:systemsn:jeffrey 浏览器中输入 ...

  6. github相关问题

    一.项目编译打包后生成的dist文件夹后:项目提交到github上dist文件提交不上去. 在.gitignore文件,删除一行 二.更改github的语言属性 .gitattributes文件:若项 ...

  7. 优化你的java代码性能

    一.避免在循环条件中使用复杂表达式 在不做编译优化的情况下,在循环中,循环条件会被反复计算,如果不使用复杂表达式,而使循环条件值不变的话,程序将会运行的更快. 例子:import java.util. ...

  8. 问题003:JDK文件夹下的bin有什么作用?javac.exe和java.exe双击后为什么一闪而过,没了?

    bin (binary)二进制 ,JDK当中所有的可以执行的二进制应用程序都放在其中.其中都是*.exe文件,表示可以直接执行程序. javac.exe和java.exe双击后为什么一闪而过,没了?因 ...

  9. 洛谷P2908 [USACO08OPEN]文字的力量Word Power

    题目描述 Farmer John wants to evaluate the quality of the names of his N (1 <= N <= 1000) cows. Ea ...

  10. POJ 2774 后缀数组 || 二分+哈希

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 35607   Accepted: 14 ...