当前位置:网站首页>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;
}
}
边栏推荐
- [batch dos-cmd command - summary and summary] - commands related to Internet access and network communication (Ping, Telnet, NSLOOKUP, ARP, tracert, ipconfig)
- VectorDraw Developer Framework 10.10
- Tupu software digital twin 3D wind farm, offshore wind power of smart wind power
- Shell tips (134) simple keyboard input recorder
- Selection of Hongmeng page menu
- VectorDraw Developer Framework 10.10
- STL tutorial 4- input / output stream and object serialization
- (tool class) quickly add time to code in source insight
- [leetcode] two num · sum of two numbers
- realsense d455 semantic_ Slam implements semantic octree mapping
猜你喜欢

realsense d455 semantic_slam实现语义八叉树建图

【蒸馏】PointDistiller: Structured Knowledge DistillationTowards Efficient and Compact 3D Detection

14 BS object Node name Name attrs string get node name attribute content

Advanced mathematics foundation_ Parity of functions

Chuantu microelectronics breaks through the high-end isolator analog chip market with ca-is3062w

Genuine photoshop2022 purchase experience sharing

Five causes of PCB board deformation and six solutions 2021-10-08
![Different paths ii[dynamic planning improvement for DFS]](/img/bb/1e1cee22b9de954de242d299a1a0eb.png)
Different paths ii[dynamic planning improvement for DFS]
![[Batch dos - cmd Command - Summary and Summary] - cmd extension Command, extension Function (CMD / E: on, CMD / E: off)](/img/2b/4495a6cd41a2dd4e7a20ee60b398c9.png)
[Batch dos - cmd Command - Summary and Summary] - cmd extension Command, extension Function (CMD / E: on, CMD / E: off)

My debut is finished!
随机推荐
(tool class) quickly add time to code in source insight
Sichuan earth microelectronics ca-is1300 isolated operational amplifier for current detection is on the market
Ltpowercad II and ltpowerplanner III
“空间转换”显著提升陡崖点云的地面点提取质量
RTKLIB-b33版本中GALILEO广播星历存储问题
C#获取exe的版本号-文件版本and程序集版本
Path planner based on time potential function in dynamic environment
三年营收连续下滑,天地壹号困在醋饮料里
14 BS object Node name Name attrs string get node name attribute content
Vscode official configuration synchronization scheme
ELK + filebeat日志解析、日志入库优化 、logstash过滤器配置属性
Misunderstanding of switching triode
【批处理DOS-CMD命令-汇总和小结】-外部命令-cmd下载命令、抓包命令(wget)
诸葛亮 VS 庞统,拿下分布式 Paxos
MySQL facet 01
Research on 3D model retrieval method based on two channel attention residual network - Zhou Jie - paper notes
VectorDraw Developer Framework 10.10
[batch dos-cmd command - summary and summary] - CMD window setting and operation commands (CD, title, mode, color, pause, CHCP, exit)
useMemo模拟useCallback
What is the difference between norflash and nandflash