当前位置:网站首页>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
边栏推荐
- net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
- [untitled]
- SQL tuning series - Introduction to SQL tuning
- CSP certification 202203-2 travel plan (multiple solutions)
- 域名和IP地址的联系
- 59、螺旋矩阵(数组)
- JUC concurrent programming 09 -- source code analysis of condition implementation
- Sim Api User Guide(5)
- Art template template engine
- ARM调试(1):两种在keil中实现printf重定向到串口的方法
猜你喜欢
解决VMware卸载后再安装出现的问题
Exercise questions and simulation test of refrigeration and air conditioning equipment operation test in 2022
Detailed explanation of MapReduce calculation process
Reading integrity monitoring techniques for vision navigation systems - 3 background
Sim Api User Guide(4)
0704、ansible----01
Failureforwardurl and failureurl
0704、ansible----01
Question bank and answers of Shanghai safety officer C certificate examination in 2022
Custom login failure handling
随机推荐
第一章 Oracle Database In-Memory 相关概念(续)(IM-1.2)
C语言——自定义类型
DBA common SQL statements (1) - overview information
Detailed explanation of MapReduce calculation process
Configuration of LNMP
Common SQL statements of DBA (6) - daily management
206、反转链表(链表)
Ansible cloud computing automation
深度选择器
203. Remove linked list elements (linked list)
SQL调优系列文章之—SQL调优简介
杰理之通常影响CPU性能测试结果的因素有:【篇】
Sim Api User Guide(6)
1、两数之和(哈希表)
209、长度最小的子数组(数组)
第120章 SQL函数 ROUND
Longest common front string
【无标题】
SQL tuning series - Introduction to SQL tuning
997、有序数组的平方(数组)