当前位置:网站首页>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 .
边栏推荐
- ACLY与代谢性疾病
- C#依赖注入(直白明了)讲解 一看就会系列
- The Missing Semester
- How to understand the developed query statements
- C summary of knowledge points 3
- MQ-防止消息丢失及重复消费
- CPU 上下文切换的机制和类型 (CPU Context Switch)
- Istio, ebpf and rsocket Broker: in depth study of service grid
- redis中value/String
- Xiaomi mobile phone unlocking BL tutorial
猜你喜欢
随机推荐
How to understand the developed query statements
对于mvvm和mvc的理解
Harbor webhook from principle to construction
耐克如何常年霸榜第一名?最新財報答案來了
Why must we move from Devops to bizdevops?
强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
小米手机解BL锁教程
Istio, ebpf and rsocket Broker: in depth study of service grid
今天开户今天能买股票吗?在线开户是很安全么?
GID: open vision proposes a comprehensive detection model knowledge distillation | CVPR 2021
Dlhsoft Kanban, Kanban component of WPF
The specified service is marked for deletion
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
Exposure: a white box photo post processing framework reading notes
Xiaomi mobile phone unlocking BL tutorial
C summary of knowledge points 1
Redis的攻击手法
Joint Time-Frequency and Time Domain Learning for Speech Enhancement
消息队列之监控退款任务批处理过程
The Missing Semester









