当前位置:网站首页>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 .
边栏推荐
- 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
- ABBIRB120工业机器人机械零点位置
- Value/sortedset in redis
- C#依赖注入(直白明了)讲解 一看就会系列
- 如何看懂开发的查询语句
- Skip the test cases to be executed in the unittest framework
- VScode快捷键(最全)[通俗易懂]
- Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)
- How does Nike dominate the list all the year round? Here comes the answer to the latest financial report
- redis常识
猜你喜欢
【单片机】【数码管】数码管显示
Computer graduation project asp Net attendance management system vs developing SQLSERVER database web structure c programming computer web page source code project
Abbirb120 industrial robot mechanical zero position
基于IMDB评论数据集的情感分析
Harbor webhook from principle to construction
Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation
技术分享 | MySQL:从库复制半个事务会怎么样?
图的理论基础
深入理解 grpc part1
Botu V15 add GSD file
随机推荐
241. 为运算表达式设计优先级 : DFS 运用题
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
Unity xlua co process packaging
Implementation of address book management system with C language
区间乘积的因子数之和——前缀和思想+定一移二
CPU 上下文切换的机制和类型 (CPU Context Switch)
redis中value/set
Talk about biological live broadcast - genovis Zhang Hongyan antibody specific enzyme digestion technology helps to characterize the structure of antibody drugs
GID:旷视提出全方位的检测模型知识蒸馏 | CVPR 2021
CAD如何设置标注小数位
Are the consequences of securities account cancellation safe
Chen Gong: Micro service, is it still so pure?
想问问,证券开户有优惠吗手机开户是安全么?
Talk about the pessimistic strategy that triggers full GC?
Force button homepage introduction animation
uniapp 使用 uni-upgrade-center
Redis startup and library entry
Why must we move from Devops to bizdevops?
Emotion analysis based on IMDB comment data set
Learning summary on June 28, 2022