当前位置:网站首页>LeetCode_哈希表_中等_454.四数相加 II
LeetCode_哈希表_中等_454.四数相加 II
2022-06-25 06:40:00 【一瓢江湖我沉浮】
1.题目
给你四个整数数组 nums1、nums2、nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足:
0 <= i, j, k, l < n
nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
示例 1:
输入:nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
输出:2
解释:
两个元组如下:
(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
示例 2:
输入:nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
输出:1
提示:
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
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/4sum-ii
2.思路
(1)暴力穷举法
该方法比较简单也易于想到,使用 4 个 for 循环穷举所有元组的组合并进行判断即可,使用变量 res 来记录符合条件的元组。但是该方法时间复杂度太高(为O(n4)),显然这在LeetCode 上提交时会给出“超出时间限制”的提示!
(2)哈希表_分组
思路参考本题官方题解。
3.代码实现(Java)
//思路1————暴力穷举法
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;
}
}
//思路2————哈希表_分组
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;
}
}
边栏推荐
- LeetCode 每日一题——515. 在每个树行中找最大值
- OAuth 2.0一键登录那些事
- 國外LEAD域名郵箱獲取途徑
- Modular programming of digital light intensity sensor module gy-30 (main chip bh1750fvi) controlled by single chip microcomputer (under continuous updating)
- 指南针可以开股票账户吗?安全吗?
- 基于地面点稀少的LiDAR点云的茂密森林蓄积量估算
- 不同路径II[针对DFS的动态规划改进]
- VectorDraw Developer Framework 10.10
- Five causes of PCB board deformation and six solutions 2021-10-08
- [distillation] pointdistiller: structured knowledge distillationwards efficient and compact 3D detection
猜你喜欢

How comfortable it is to use Taijiquan to talk about distributed theory!

Distributed quorum NWR of the alchemy furnace of the Supreme Master

Why "New Year's Eve", the original memory burst!

数据可视化没有重点怎么办?

Summary of small problems in smartbugs installation

Find out what informatization is, and let enterprises embark on the right path of transformation and upgrading
![[batch dos-cmd command - summary and summary] - CMD extended command and function (CMD /e:on, CMD /e:off)](/img/2b/4495a6cd41a2dd4e7a20ee60b398c9.png)
[batch dos-cmd command - summary and summary] - CMD extended command and function (CMD /e:on, CMD /e:off)

Zhugeliang vs pangtong, taking distributed Paxos
![Insert and sort the linked list [dummy unified operation + broken chain core - passive node]](/img/2a/ccb1145d2b4f9fbd8d0812deace93b.png)
Insert and sort the linked list [dummy unified operation + broken chain core - passive node]

14 BS object Node name Name attrs string get node name attribute content
随机推荐
My debut is finished!
realsense d455 semantic_ Slam implements semantic octree mapping
Selection of Hongmeng page menu
Three years of continuous decline in revenue, Tiandi No. 1 is trapped in vinegar drinks
【Qt】快捷键
Harmony food menu interface
The method of judging whether triode can amplify AC signal
Sichuan earth microelectronics high performance, high integration and low cost isolated 485 transceiver
Evolution of Alibaba e-commerce architecture
Let's talk about MCU crash caused by hardware problems
Insert and sort the linked list [dummy unified operation + broken chain core - passive node]
基于激光雷达的林业调查常用术语及含义锦集
双三次差值bicubic
Chuantuwei ca-is3720lw alternative material No. iso7820fdw
Pytorch遇到的坑:为什么模型训练时,L1loss损失无法下降?
用太极拳讲分布式理论,真舒服!
Storage of Galileo broadcast ephemeris in rtklib-b33
音频(五)音频特征提取
图扑软件数字孪生 3D 风电场,智慧风电之海上风电
VectorDraw Developer Framework 10.10