当前位置:网站首页>LeetCode_ Hash table_ Medium_ 454. adding four numbers II
LeetCode_ Hash table_ Medium_ 454. adding four numbers II
2022-06-25 08:12:00 【I've been up and down in the Jianghu】
1. subject
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 :
(0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
(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
source : Power button (LeetCode)
link :https://leetcode.cn/problems/4sum-ii
2. Ideas
(1) The law of violent exhaustion
This method is simple and easy to think of , Use 4 individual for Loop enumerates all tuple combinations and makes judgments , Using variables res To record qualified tuples . But the time complexity of this method is too high ( by O(n4)), Obviously this is LeetCode Will be given when submitting “ Time limit exceeded ” A hint of !
(2) Hashtable _ grouping
Train of thought reference Official solution to this problem .
3. Code implementation (Java)
// Ideas 1———— The law of violent exhaustion
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
int n = nums1.length;
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
for (int l = 0; l < n; l++) {
if (nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0) {
res++;
}
}
}
}
}
return res;
}
}
// Ideas 2———— Hashtable _ grouping
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
int res = 0;
Map<Integer, Integer> cnt12 = new HashMap<>();
for (int num1 : nums1) {
for (int num2 : nums2) {
cnt12.put(num1 + num2, cnt12.getOrDefault(num1 + num2, 0) + 1);
}
}
for (int num3 : nums3) {
for (int num4 : nums4) {
if (cnt12.containsKey(-num3 - num4)) {
res += cnt12.get(-num3 - num4);
}
}
}
return res;
}
}
边栏推荐
- 深度学习系列48:DeepFaker
- 洛谷P2048 [NOI2010] 超级钢琴(RMQ+优先队列)
- Import data into Matlab
- [supplementary question] 2021 Niuke summer multi school training camp 1-3
- Electronics: Lesson 010 - Experiment 9: time and capacitors
- To understand the difference between Gram-positive and Gram-negative bacteria and the difference in pathogenicity
- MySQL simple permission management
- 测一测现在的温度
- 企业全面云化的时代——云数据库的未来
- 不怕百战失利,就怕灰心丧气
猜你喜欢

五分钟快速搭建一个实时人脸口罩检测系统(OpenCV+PaddleHub 含源码)

Deep learning series 48:deepfaker

Electronics: Lesson 013 - Experiment 14: Wearable pulsed luminaries

Electronics: Lesson 011 - experiment 10: transistor switches

Electronics: Lesson 010 - Experiment 9: time and capacitors

Electronics: Lesson 012 - Experiment 11: light and sound

双周投融报:资本埋伏Web3基础设施

PH neutralization process modeling

网络模型——OSI模型与TCP/IP模型

电子学:第013课——实验 14:可穿戴的脉冲发光体
随机推荐
函数尽量不要通过变量指定操作类型
DNS协议及其DNS完整的查询过程
Stm32cubemx learning (5) input capture experiment
黑点==白点(MST)
Sword finger offer (simple level)
Use the frame statistics function of the message and waveform recording analyzer royalscope to troubleshoot the accidental faults of the CAN bus
测一测现在的温度
Allgero reports an error: program has encoded a problem and must exit The design will be saved as a . SAV file
电子学:第012课——实验 13:烧烤 LED
Modeling and fault simulation of aircraft bleed system
[supplementary question] 2021 Niuke summer multi school training camp 4-N
Looking for b-end product manager after years? I almost ruined myself
Socket problem record
Solving some interesting problems with recurrence of function
Deep learning series 45: overview of image restoration
Not afraid of losing a hundred battles, but afraid of losing heart
STM32CubeMX 学习(5)输入捕获实验
Electronics: Lesson 011 - experiment 10: transistor switches
【莫比乌斯反演】
Can transparent cloud gateway caniot and candtu record can messages and send and receive can data remotely