当前位置:网站首页>代码随想录笔记_哈希_454四数相加II
代码随想录笔记_哈希_454四数相加II
2022-07-31 13:12:00 【Erik_Won】
代码随想录笔记_哈希表
代码随想录二刷笔记记录
LC 454.四数相加II
题目
给你四个整数数组 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
解释:
两个元组如下:
- (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
- (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 ,都是一样的。因此我们易知,遍历每个数组的长度是相等的。
因此只需要依次遍历4个数组
首先遍历 A数组 和 B数组,map的 key 和 value 分别记录 A数组和B数组的 sum,value 记录sum出现的次数
之后遍历 C数组 和 D数组, 将 0-(c+d) 作为 key ,在 map 中查找,如果存在 key,则代表 a+b+c+d = 0 ,存在四数相加 = 0 的元组
算法步骤
1.定义HashMap,key 存放 sum = a + b ,value 存放 sum 出现的次数
2.遍历数组A 和 B,统计两个数的sum,和sum出现的次数
3.维护变量 count 记录 a+b+c+d = 0 出现的次数
4.遍历数组C 和 D,记录 0-(c+d) ,在 map 中出现过的话,则加上 count
5.最后返回统计值 count
推演分析:
代码实现
完整代码实现
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer, Integer> map = new HashMap<>();
int res = 0;
//step1:统计前两个数组之和,及和出现的次数
for(int i = 0;i<nums1.length;i++){
for(int j= 0;j<nums2.length;j++){
int sumAB = nums1[i]+nums2[j];
if(map.containsKey(sumAB)) map.put(sumAB,map.get(sumAB)+1);
else map.put(sumAB,1);
}
}
//step2:统计后两个数组之和,并判断和的相反数是否存在于Map中
for(int i = 0;i<nums3.length;i++){
for(int j = 0;j<nums4.length;j++){
int sumCD = -(nums3[i]+nums4[j]);
if(map.containsKey(sumCD)) res += map.get(sumCD);
}
}
return res;
}
边栏推荐
- ASM module in SAP Ecommerce Cloud Spartacus UI and Accelerator UI
- 电脑重要文件很多,如何备份比较安全?
- 【牛客刷题-SQL大厂面试真题】NO3.电商场景(某东商城)
- 0X7FFFFFFF,0X80000000「建议收藏」
- Four ways to clear the float and its principle understanding
- Detailed explanation of network protocols and related technologies
- 查看Mysql数据库版本
- 网络协议及相关技术详解
- Edge Cloud Explained in Simple Depth | 4. Lifecycle Management
- NameNode故障处理的两种方法
猜你喜欢
CentOS7 —— yum安装mysql
阿里三面:MQ 消息丢失、重复、积压问题,怎么解决?
Reasons and solutions for Invalid bound statement (not found)
C# using NumericUpDown control
C#获得网卡信息 NetworkInterface IPInterfaceProperties
Install the latest pytorch gpu version
IDEA找不到Database解决方法
Solution for browser hijacking by hao360
C# control ListView usage
Network layer key protocol - IP protocol
随机推荐
使用openssl命令生成证书和对应的私钥,私钥签名,公钥验签
go使用makefile脚本编译应用
The operator,
SAP message TK 248 solved
The importance of strategic offensive capability is much higher than strategic defensive capability
IDEA的database使用教程(使用mysql数据库)
centos7安装mysql5.7步骤(图解版)
生产力工具和插件
中望3D 2023正式发布,设计仿真制造一体化缩短产品开发周期
CentOS7 —— yum安装mysql
go中select语句
selenium被反爬了怎么办?
【牛客刷题-SQL大厂面试真题】NO3.电商场景(某东商城)
C#控件 ToolStripProgressBar 用法
Detailed explanation of network protocols and related technologies
Six Stones Programming: No matter which function you think is useless, people who can use it will not be able to leave, so at least 99%
Introduction to using NPM
查看Oracle数据库的用户名和密码
报错IDEA Terminated with exit code 1
alert(1) (haozi.me)靶场练习