当前位置:网站首页>LeetCode 454. Add four numbers II
LeetCode 454. Add four numbers II
2022-07-01 11:59:00 【Grilled little fat sheep with charcoal...】
Title Description : Here are four integer arrays nums1、nums2、nums3 and nums4 , The length of the array is n , Please calculate how many tuples there are (i, j, k, l) To meet the :
0 <= i, j, k, l < n
nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
Example 1:
Input :nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
Output :2
explain :
Two tuples are as follows :
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
Example 2:
Input :nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
Output :1
Tips :
n == nums1.length
n == nums2.length
n == nums3.length
n == nums4.length
1 <= n <= 200
-228 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 228
Thinking analysis :
- First define One map aggregate ,key discharge a and b Sum of two numbers ,value discharge a and b The number of times the sum of two numbers appears .
- Traverse num1 and nums2 Array , Count the sum of two array elements , And the number of times , Put it in map in .
- Definition int Variable res, Used for statistical a+b+c+d = 0 Number of occurrences .
- In a traverse nums3 and nums4 Array , Find out if 0-(c+d) stay map If you've seen it in the past , Just use res hold map in key Corresponding value That is, the number of occurrences .
- Finally, the statistics are returned res That's all right.
Code implementation :
public static 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 sum of the remaining two elements , stay map Find out if there is an addition to 0 The situation of , Record the number of times 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;
}
source : Power button (LeetCode)
link :https://leetcode.cn/problems/4sum-ii
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
边栏推荐
- 我在中山,到哪里开户比较好?实际上网上开户安全么?
- 强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
- Brief explanation of the working principle, usage scenarios and importance of fingerprint browser
- On recursion and Fibonacci sequence
- 超详细黑苹果安装图文教程送EFI配置合集及系统
- 消息队列之监控退款任务批处理过程
- 图的理论基础
- ABBIRB120工业机器人机械零点位置
- Redis的攻击手法
- Wechat applet development - user authorization to log in to "suggestions collection"
猜你喜欢

On recursion and Fibonacci sequence

Talk about biological live broadcast - genovis Zhang Hongyan antibody specific enzyme digestion technology helps to characterize the structure of antibody drugs

Rural guys earn from more than 2000 a month to hundreds of thousands a year. Most brick movers can walk my way ǃ

C summary of knowledge points 1

Why must we move from Devops to bizdevops?

C knowledge point form summary 2

S7-1500plc simulation

NOV Schedule for . Net to display and organize appointments and recurring events

Redis的攻击手法

GID:旷视提出全方位的检测模型知识蒸馏 | CVPR 2021
随机推荐
Can I open an account today and buy stocks today? Is it safe to open an account online?
技术分享 | MySQL:从库复制半个事务会怎么样?
redis常识
Question: what professional qualities should test engineers have?
Rural guys earn from more than 2000 a month to hundreds of thousands a year. Most brick movers can walk my way ǃ
About keil compiler, "file has been changed outside the editor, reload?" Solutions for
Explore the contour detection function findcontours() of OpenCV in detail with practical examples, and thoroughly understand the real role and meaning of each parameter and mode
小米手机解BL锁教程
ACLY与代谢性疾病
Talk about the pessimistic strategy that triggers full GC?
Learning summary on June 30, 2022
Adjacency matrix undirected graph (I) - basic concepts and C language
Golang des-cbc
想问问,证券开户有优惠吗手机开户是安全么?
MQ-防止消息丢失及重复消费
Exploration and practice of inress in kubernetes
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
NOV Schedule for . Net to display and organize appointments and recurring events
8 best practices to protect your IAC security!
Matrix of numpy