当前位置:网站首页>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 .
边栏推荐
- How to make the development of liquidity pledge mining system, case analysis and source code of DAPP defi NFT LP liquidity pledge mining system development
- 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
- Value/sortedset in redis
- 消息队列之监控退款任务批处理过程
- [classic example] classic list questions @ list
- 耐克如何常年霸榜第一名?最新财报答案来了
- ES6 promise Usage Summary
- redis中value/hush
- I'm in Zhongshan. Where is a better place to open an account? Is it actually safe to open an account online?
- [Maui] add click events for label, image and other controls
猜你喜欢

C knowledge point form summary 2

Uniapp uses uni upgrade Center

二叉堆(一) - 原理与C实现

自组织是管理者和成员的双向奔赴

技术分享 | MySQL:从库复制半个事务会怎么样?

Seckill system 03 - redis cache and distributed lock

Dlhsoft Kanban, Kanban component of WPF

How to understand the developed query statements

ACLY与代谢性疾病

强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
随机推荐
我在中山,到哪里开户比较好?实际上网上开户安全么?
2022/6/29学习总结
VScode快捷键(最全)[通俗易懂]
Neo4j 中文开发者月刊 - 202206期
Kafuka learning path (I) Kafuka installation and simple use
[Maui] add click events for label, image and other controls
Deep understanding of grpc part1
想问问,证券开户有优惠吗手机开户是安全么?
2022/6/30学习总结
自组织是管理者和成员的双向奔赴
redis中value/String
Are the consequences of securities account cancellation safe
用实际例子详细探究OpenCV的轮廓检测函数findContours(),彻底搞清每个参数、每种模式的真正作用与含义
Mechanism and type of CPU context switch
Force button homepage introduction animation
redis中value/list
Dataset partitioning script for classification tasks
GID: open vision proposes a comprehensive detection model knowledge distillation | CVPR 2021
The specified service is marked for deletion
Value/hush in redis