当前位置:网站首页>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
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
边栏推荐
- I'm in Zhongshan. Where is a better place to open an account? Is it actually safe to open an account online?
- Why does the JVM heap memory exceed 32g and pointer compression fail?
- [MCU] [nixie tube] nixie tube display
- Summary of JFrame knowledge points 1
- solo 可以通过 IPV6 访问吗?
- Learning summary on June 28, 2022
- Comment Cao définit la décimale de dimension
- CPU 上下文切换的机制和类型 (CPU Context Switch)
- 深入理解 grpc part1
- 印象深刻的bug汇总(持续更新)
猜你喜欢

使用set_handler过滤掉特定的SystemC Wraning &Error Message

The Missing Semester

深入理解 grpc part1

2022/6/29学习总结

Deep understanding of grpc part1

Software project management 9.2 Software project configuration management process

如何看懂开发的查询语句

Learning summary on June 29, 2022

CAD如何设置标注小数位

Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)
随机推荐
自组织是管理者和成员的双向奔赴
Abbirb120 industrial robot mechanical zero position
The Missing Semester
Personnaliser le plug - in GRPC
Custom grpc plug-in
Activity workflow engine
流动性质押挖矿系统开发如何制作,dapp丨defi丨nft丨lp流动性质押挖矿系统开发案例分析及源码
How to make the development of liquidity pledge mining system, case analysis and source code of DAPP defi NFT LP liquidity pledge mining system development
Binary stack (I) - principle and C implementation
redis中value/set
VScode快捷键(最全)[通俗易懂]
邻接矩阵无向图(一) - 基本概念与C语言
区间乘积的因子数之和——前缀和思想+定一移二
Vscode shortcut key (the most complete) [easy to understand]
Kafuka learning path (I) Kafuka installation and simple use
I'm in Zhongshan. Where is a better place to open an account? Is it actually safe to open an account online?
深入理解 grpc part1
Width and widthstep of iplimage
用于分类任务的数据集划分脚本
Mechanism and type of CPU context switch