当前位置:网站首页>454、四数之和(哈希表)
454、四数之和(哈希表)
2022-04-23 10:11:00 【Popuessing's Jersey】
题目:
给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0。
为了使问题简单化,所有的 A, B, C, D 具有相同的长度 N,且 0 ≤ N ≤ 500 。所有整数的范围在 -2^28 到 2^28 - 1 之间,最终结果不会超过 2^31 - 1。
方法:哈希表法
思路:将四个数组两两分组,记录其中两个数组之和以及他们 和的出现次数,并将他们以和的值为key,出现次数为value存放在哈希表中,
另外两个数组和的值作为key去寻找哈希表中是否存在key使得四个数组的和为0,即map.containKey(0-nums3[i]+nums4[j])
如果找到了符合条件的key值,就更新出现次数,遍历完后返回最后的出现次数
import java.util.HashMap;
import java.util.Map;
public class Sishuzhihe2 {
//首先定义一个hashmap,key存放a和b两数之和,value放a和b两数之和出现的次数
//遍历A,B两数组,统计量数组元素之和出现的次数,并放入map
//定义int变量count,统计a+b+c+d=0出现的次数
//在遍历C和D数组,如果找到0-(c+d)在map中出现过的话,就用count把map中key对应的value也就是出现次数统计出来
//返回count
public 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中找到是否存在key使得,相加为0,同时记录出现次数
for (int i : nums3) {
for (int j : nums4) {
temp= i+j;
if (map.containsKey(0-temp)){
res+= map.get(0-temp);
}
}
}
return res;
}
public static void main(String[] args) {
int [] a = {1,2};
int [] b = {-2,-1};
int [] c = {-1,2};
int [] d = {0,2};
Sishuzhihe2 sishuzhihe2 = new Sishuzhihe2();
int res = sishuzhihe2.fourSumCount(a,b,c,d);
System.out.println(res);
}
}
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://blog.csdn.net/CoCo629vanilla/article/details/121478588
边栏推荐
- 0704、ansible----01
- Realizing data value through streaming data integration (4) - streaming data pipeline
- DBA常用SQL语句(6)- 日常管理
- 杰理之系统事件有哪些【篇】
- 《Redis设计与实现》
- 通过流式数据集成实现数据价值(5)- 流处理
- Ansible cloud computing automation
- 工业元宇宙平台规划与建设
- [COCI] lattice (dichotomy + tree divide and conquer + string hash)
- Realize data value through streaming data integration (1)
猜你喜欢

JUC concurrent programming 07 -- is fair lock really fair (source code analysis)

Sim Api User Guide(4)

2022茶艺师(初级)考试试题模拟考试平台操作

Shell script interaction free

从知识传播的维度对比分析元宇宙

ARM调试(1):两种在keil中实现printf重定向到串口的方法

C language: expression evaluation (integer promotion, arithmetic conversion...)

"Gu Yu series" airdrop

The central control learning infrared remote control module supports network and serial port control

Sim Api User Guide(4)
随机推荐
SQL调优系列文章之—SQL调优简介
ansible 云计算 自动化
杰理之系统事件有哪些【篇】
最长公共前串
Yarn资源调度器
Odoo 服务器搭建备忘
2022年广东省安全员A证第三批(主要负责人)考试试题及答案
Understand the new economic model of platofarm and its ecological progress
Art template template engine
Realize data value through streaming data integration (3) - real-time continuous data collection
第三章 启用和调整IM列存储的大小(IM-3.1)
打印页面的功能实现
Formattime timestamp format conversion
DBA常用SQL语句(2)— SGA和PGA
The central control learning infrared remote control module supports network and serial port control
第二章 In-Memory 体系结构 (IM-2.2)
P1446 [hnoi2008] cards (Burnside theorem + DP count)
元宇宙时代的职业规划与执行
formatTime时间戳格式转换
Failureforwardurl and failureurl