4Sum II
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的更多相关文章
- LeetCode 454. 4Sum II
454. 4Sum II Add to List Description Submission Solutions Total Accepted: 8398 Total Submissions: 18 ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- 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), ...
随机推荐
- 【Unity3D】简要分析unity3d中剪不断理还乱的yield
在学习unity3d的时候很容易看到下面这个例子: void Start () { StartCoroutine(Destroy()); } IEnumerator Destroy(){ yield ...
- ADODB.Stream在进行文件上传时报错
最近在做web项目,有个控件是上传材料文件和文件夹,本地运行正常,放到服务器上,一直报错:AutoRuntime服务器无法创建..... 解决方法: 1.配置ie浏览器的安全级别 2.修改ie浏览器对 ...
- php允许被跨域ajax请求
只要在被请求端,加一句: header('Access-Control-Allow-Origin: *');
- springmvc之Hello World及常用注解
步骤: 加入jar包 在web.xml 中配置DispacherServlet 加入SpringMVC 配置文件springmvc.xml 编写请求处理器(action/controller) 编写视 ...
- 实现简单Restful API
1. 首选我们通过 http://start.spring.io/ 网址生成一个基础spring boot 项目,截图配置如下: 点击 generate Project 按钮生成并下载基础项目 2. ...
- zabbix中监控项报错
报错信息: zabbix报错(Not all processes could be identified, non-owned process info will not be shown, you ...
- Hyper-V 2016 配置管理系列(应用篇)
远程连接到Hyper-V HOST 为了日常运维管理操作,使用远程PowerShell工作.Windows 10上安装了RSAT(远程管理工具 ).然后安装了Hyper-V控制台: 在能够远程连接到H ...
- jenkins代码自动部署
jenkins是一个广泛用于持续构建的可视化web工具,持续构建说得更直白点,就是各种项目的"自动化"编译.打包.分发部署.jenkins可以很好的支持各种语言(比如:java, ...
- win10搜索不到蓝牙设备
多半是驱动不兼容的问题. 解决方法: 此电脑右键,设备管理器,然后将蓝牙下的驱动,右键.卸载设备. 安装驱动精灵,会自动检测到缺少蓝牙驱动,安装即可.
- idea字体模糊
用jdk1.8的jre替换idea的jre64,但是记得在lib里加上jdk的lib中的tools.jar. 如图: 然后 将原来jre64的TOOLS.jar拷贝到替换后的jre的lib目录下,重启 ...