当前位置:网站首页>代码随想录笔记_哈希_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;
}
边栏推荐
- 365天挑战LeetCode1000题——Day 044 最大层内元素和 层次遍历
- Solution for browser hijacking by hao360
- sqlalchemy 判断一个array 类型的字段是否和一个array有至少一个一致的数据
- 知名无人驾驶公司:文远知行内推
- ECCV2022:在Transformer上进行递归,不增参数,计算量还少!
- 基本语法(二)
- Google Chrome(谷歌浏览器)安装使用
- 网络层重点协议——IP协议
- [RPI]树莓派监控温度及报警关机保护「建议收藏」
- SAP 电商云 Spartacus SSR Optimization Engine 几处 timeout 的执行顺序
猜你喜欢

五种数据提交方式的优化

网络层重点协议——IP协议

Hard disk partition, expand disk C, no reshipment system, not heavy D dish of software full tutorial.

Google Chrome(谷歌浏览器)安装使用

golang-gin - graceful restart

Centos7 install mysql5.7 steps (graphical version)

PyQt5快速开发与实战 10.1 获取城市天气预报

How IDEA runs web programs

ECCV 2022 | 机器人的交互感知与物体操作

go使用makefile脚本编译应用
随机推荐
Selenium IDE for Selenium Automation Testing
架构实战营|模块8
计算机复试面试问题(计算机面试常见问题)
CentOS7 —— yum安装mysql
ICML2022 | 面向自监督图表示学习的全粒度自语义传播
IDEA连接MySQL数据库并执行SQL查询操作
C# control ListView usage
PyQt5快速开发与实战 9.7 UI层的自动化测试
PHP序列化:eval
numpy矩阵和向量的保存与加载,以及使用保存的向量进行相似度计算
C# control StatusStrip use
NameNode (NN) 和SecondaryNameNode (2NN)工作机制
Ali on three sides: MQ message loss, repetition, backlog problem, how to solve?
sqlalchemy determines whether a field of type array has at least one consistent data with an array
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
使用openssl命令生成证书和对应的私钥,私钥签名,公钥验签
Anaconda安装labelImg图像标注软件
Spark学习:为Spark Sql添加自定义优化规则
STM32——软件SPI控制AD7705[通俗易懂]
/run/NetworkManager占用空间过大