当前位置:网站首页>454. Sum of four numbers (hash table)
454. Sum of four numbers (hash table)
2022-04-23 10:16:00 【Popuessing's Jersey】
subject :
Given a list of four arrays containing integers A , B , C , D , Calculate how many tuples there are (i, j, k, l) , bring A[i] + B[j] + C[k] + D[l] = 0.
To simplify the problem , be-all A, B, C, D Same length N, And 0 ≤ N ≤ 500 . All integers are in the range of -2^28 To 2^28 - 1 Between , The final result will not exceed 2^31 - 1.
Method : Hash table method
Ideas : Group four arrays in pairs , Record the sum of two arrays and their Number of occurrences of and , And take the value of sum as key, The number of occurrences is value Stored in hash table ,
The values of the other two arrays and are used as key To find out if the hash table exists key Make the sum of the four arrays 0, namely map.containKey(0-nums3[i]+nums4[j])
If you find a qualified key value , On the number of updates , After traversal, return the last occurrence number
import java.util.HashMap;
import java.util.Map;
public class Sishuzhihe2 {
// So let's define a hashmap,key Deposit a and b Sum of two numbers ,value discharge a and b The number of times the sum of two numbers appears
// Traverse A,B Two arrays , The number of occurrences of the sum of statistics array elements , And put in map
// Definition int Variable count, Statistics a+b+c+d=0 Number of occurrences
// In a traverse C and D Array , If you find 0-(c+d) stay map If you've seen it in the past , Just use count hold map in key Corresponding value That is, the number of occurrences
// return count
public int fourSumCount(int [] nums1,int[] nums2,int[] nums3,int[] nums4){
Map<Integer,Integer> map = new HashMap<>();
int temp;
int res = 0;
// Count the sum of the elements in two arrays , At the same time, count the number of occurrences , Put in 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);
}
}
}
// Count the elements and... Of the remaining two arrays , stay map Found in key bring , Add to 0, Record the number of occurrences at the same time
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://yzsam.com/2022/04/202204231011140704.html
边栏推荐
- Understand the new economic model of platofarm and its ecological progress
- art-template 模板引擎
- Windows installs redis and sets the redis service to start automatically
- Ansible playbook syntax and format automate cloud computing
- 杰理之有时候发现内存被篡改,但是没有造成异常,应该如何查找?【篇】
- 解决方案架构师的小锦囊 - 架构图的 5 种类型
- Realize data value through streaming data integration (1)
- 解决VMware卸载后再安装出现的问题
- Ansible cloud computing automation command line compact version
- [untitled]
猜你喜欢
101. Symmetric Tree
深度选择器
解决VMware卸载后再安装出现的问题
Windows installs redis and sets the redis service to start automatically
Jerry's more accurate determination of abnormal address [chapter]
Redis design and Implementation
Solve the problem of installing VMware after uninstalling
Six practices of Windows operating system security attack and defense
Sim Api User Guide(6)
【无标题】
随机推荐
IDEA——》每次启动都会Indexing或 scanning files to index
Yarn资源调度器
深度选择器
2022 mobile crane driver test question bank simulation test platform operation
DBA common SQL statements (2) - SGA and PGA
基于PyQt5实现弹出任务进度条功能示例
杰理之系统事件有哪些【篇】
DBA常用SQL语句(4)- Top SQL
Realizing data value through streaming data integration (5) - stream processing
JUC concurrent programming 07 -- is fair lock really fair (source code analysis)
域名和IP地址的联系
Turn: Maugham: reading is a portable refuge
Solve the problem of installing VMware after uninstalling
707. Design linked list (linked list)
Question bank and answers of Shanghai safety officer C certificate examination in 2022
第三章 启用和调整IM列存储的大小(IM-3.1)
What are Jerry's usual program exceptions? [chapter]
SQL tuning series - Introduction to SQL tuning
JUC concurrent programming 09 -- source code analysis of condition implementation
209. Subarray with the smallest length (array)