当前位置:网站首页>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;
}
}
边栏推荐
- 韩信大招:一致性哈希
- Distributed quorum NWR of the alchemy furnace of the Supreme Master
- [single chip microcomputer project training] multipoint temperature wireless acquisition system based on nRF905
- CPDA | how to start the growth path of data analysts?
- NSIS 静默安装vs2013运行时
- Intel announced five new technological developments, including quantum computing, neural pseudo computing, machine programming, integrated optoelectronics, and secure computing
- 用动图讲解分布式 Raft
- Pit encountered by pytorch: why can't l1loss decrease during model training?
- 不同路径II[针对DFS的动态规划改进]
- npm install 报错 : gyp ERR! configure error
猜你喜欢

test

My debut is finished!

Modular programming of wireless transmission module nRF905 controlled by single chip microcomputer

VectorDraw Developer Framework 10.10

realsense d455 semantic_ Slam implements semantic octree mapping

Find out what informatization is, and let enterprises embark on the right path of transformation and upgrading

Sichuan Tuwei ca-is3105w fully integrated DC-DC converter

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

Construction of occupancy grid map

STL tutorial 4- input / output stream and object serialization
随机推荐
Unity3D邪门实现之GUI下拉菜单Dropdown设计无重复项
数据可视化没有重点怎么办?
OpenMP入门
Application of point cloud intelligent drawing in intelligent construction site
[QT] shortcut key
海思3559 sample解析:vio
Application scheme | application of Sichuan earth microelectronics ca-is398x in PLC field
为什么要“除夕”,原来是内存爆了!
Chuantu microelectronics 𞓜 subminiature package isolated half duplex 485 transceiver
【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令(cd、title、mode、color、pause、chcp、exit)
Tempest HDMI leak receive 1
FairMOT yolov5s转onnx
VectorDraw Developer Framework 10.10
Let's talk about MCU crash caused by hardware problems
Genuine photoshop2022 purchase experience sharing
函数模板_类模板
Ns32f103c8t6 can perfectly replace stm32f103c8t6
Pit encountered by pytorch: why can't l1loss decrease during model training?
【批處理DOS-CMD命令-匯總和小結】-cmd擴展命令、擴展功能(cmd /e:on、cmd /e:off)
Keepalived monitors the process and automatically restarts the service process