当前位置:网站首页>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;
}
}
边栏推荐
- 搞清信息化是什么,让企业转型升级走上正确的道路
- “空间转换”显著提升陡崖点云的地面点提取质量
- Runtime——methods成员变量,cache成员变量
- 【批处理DOS-CMD命令-汇总和小结】-应用程序启动和调用、服务和进程操作命令(start、call、)
- Full range of isolator chips with integrated isolated power supply
- Harmony food menu interface
- C# 读取web上的xml
- Vscode official configuration synchronization scheme
- What is the difference between norflash and nandflash
- 诸葛亮 VS 庞统,拿下分布式 Paxos
猜你喜欢

JMeter introduction practice ----- use of global variables and local variables

What common APIs are involved in thread state changes

海思3559 sample解析:vio

图扑软件数字孪生 3D 风电场,智慧风电之海上风电

Sichuan earth microelectronics ca-is1300 isolated operational amplifier for current detection is on the market

Tempest HDMI leak receive 1

权限、认证系统相关名词概念

Modular programming of oled12864 display controlled by single chip microcomputer

Distributed quorum NWR of the alchemy furnace of the Supreme Master
![[distillation] pointdistiller: structured knowledge distillationwards efficient and compact 3D detection](/img/5c/ad42474a363c33ecc0e01890b65bbf.png)
[distillation] pointdistiller: structured knowledge distillationwards efficient and compact 3D detection
随机推荐
Cglib dynamic proxy
双三次差值bicubic
[batch dos-cmd command - summary and summary] - add comment command (REM or::)
Cocos learning diary 3 - API acquisition nodes and components
Advanced mathematics foundation_ Parity of functions
Path planner based on time potential function in dynamic environment
Introduction to Sichuan Tuwei ca-is3082wx isolated rs-485/rs-422 transceiver
Tuwei Digital Isolator and interface chip can perfectly replace imported brands Ti and ADI
Without "rice", you can cook "rice". Strategy for retrieving missing ground points under airborne lidar forest using "point cloud intelligent mapping"
Misunderstanding of switching triode
Ltpowercad II and ltpowerplanner III
Sichuan earth microelectronics ca-is1200 isolated operational amplifier for current detection
Distributed quorum NWR of the alchemy furnace of the Supreme Master
Explain distributed raft with dynamic diagram
Intel announced five new technological developments, including quantum computing, neural pseudo computing, machine programming, integrated optoelectronics, and secure computing
Application of point cloud intelligent drawing in intelligent construction site
Static bit rate (CBR) and dynamic bit rate (VBR)
一“石”二“鸟”,PCA有效改善机载LiDAR林下地面点部分缺失的困局
STL tutorial 4- input / output stream and object serialization
三年营收连续下滑,天地壹号困在醋饮料里