当前位置:网站首页>LeetCode 454. 四数相加 II
LeetCode 454. 四数相加 II
2022-07-01 11:54:00 【碳烤小肥羊。。。】
题目描述:给你四个整数数组 nums1、nums2、nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足:
0 <= i, j, k, l < n
nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
示例 1:
输入:nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
输出:2
解释:
两个元组如下:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
示例 2:
输入:nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
输出:1
提示:
n == nums1.length
n == nums2.length
n == nums3.length
n == nums4.length
1 <= n <= 200
-228 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 228
思路解析:
- 首先定义 一个map集合,key放a和b两数之和,value 放a和b两数之和出现的次数。
- 遍历num1和nums2数组,统计两个数组元素之和,和出现的次数,放到map中。
- 定义int变量res,用来统计 a+b+c+d = 0 出现的次数。
- 在遍历nums3和nums4数组,找到如果 0-(c+d) 在map中出现过的话,就用res把map中key对应的value也就是出现次数统计出来。
- 最后返回统计值 res就可以了
代码实现:
public static int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4){
Map<Integer, Integer> map = new HashMap<>();
int temp;
int res = 0;
// 统计两个数组中的元素之和,同时统计出现的次数, 放入map
for(int i : nums1){
for(int j : nums2){
temp = i + j;
if(map.containsKey(temp)){
map.put(temp, map.get(temp) + 1);
}else {
map.put(temp, 1);
}
}
}
// 统计剩下的两个元素的和,在map中找是否存在相加为0的情况,同时记录次数
for(int i : nums3){
for(int j : nums4){
temp = i + j;
if(map.containsKey(0 - temp)){
res += map.get(0-temp)
}
}
}
return res;
}
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/4sum-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
边栏推荐
- Custom grpc plug-in
- Summary of JFrame knowledge points 1
- 内核同步机制
- Binary stack (I) - principle and C implementation
- ES6 promise Usage Summary
- About keil compiler, "file has been changed outside the editor, reload?" Solutions for
- Dlhsoft Kanban, Kanban component of WPF
- The Missing Semester
- activity工作流引擎
- 流动性质押挖矿系统开发如何制作,dapp丨defi丨nft丨lp流动性质押挖矿系统开发案例分析及源码
猜你喜欢

NOV Schedule for . Net to display and organize appointments and recurring events

Dlhsoft Kanban, Kanban component of WPF

S7-1500plc simulation

Neo4j Chinese developer monthly - issue 202206

Summary of JFrame knowledge points 1

How to understand the developed query statements

Learning summary on June 30, 2022

图的理论基础
![[Maui] add click events for label, image and other controls](/img/d6/7ac9632681c970ed99c9e4d3934ddc.jpg)
[Maui] add click events for label, image and other controls
![[MCU] [nixie tube] nixie tube display](/img/5e/9e14302b4e4f5e03601392ac90479d.png)
[MCU] [nixie tube] nixie tube display
随机推荐
Neo4j 中文开发者月刊 - 202206期
VScode快捷键(最全)[通俗易懂]
Kafuka learning path (I) Kafuka installation and simple use
How does Nike dominate the list all the year round? Here comes the answer to the latest financial report
Question: what professional qualities should test engineers have?
流动性质押挖矿系统开发如何制作,dapp丨defi丨nft丨lp流动性质押挖矿系统开发案例分析及源码
Comment Nike a - t - il dominé la première place toute l'année? Voici les derniers résultats financiers.
The specified service is marked for deletion
Activity workflow engine
sshd_config 中 PermitRootLogin 的探讨
Golang根据参数引入相应配置文件的实现方式
JS日期格式化转换方法
Deep understanding of grpc part1
The Missing Semester
GID:旷视提出全方位的检测模型知识蒸馏 | CVPR 2021
树莓派4B安装tensorflow2.0[通俗易懂]
Redis' attack tactics
Botu V15 add GSD file
ES6 Promise用法小结
Redis的攻击手法