当前位置:网站首页>力扣.两数之和/四数相加||
力扣.两数之和/四数相加||
2022-07-31 05:17:00 【旺仔 小馒头】
1.两数之和
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。你可以假设每种输入只会对应一个答案。但是数组中同一个元素在答案里不能重复出现。你可以按任意顺序返回答案。
示例 1:
输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例 2:
输入:nums = [3,2,4], target = 6
输出:[1,2]
示例 3:
输入:nums = [3,3], target = 6
输出:[0,1]
在遍历数组的时候,只需要向map去查询是否有和目前遍历元素比配的数值,如果有,就找到的匹配对,如果没有,就把目前遍历的元素放进map中,因为map存放的就是我们访问过的元素。


class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
std::unordered_map <int,int> map;
for(int i = 0; i < nums.size(); i++) {
// 遍历当前元素,并在map中寻找是否有匹配的key
auto iter = map.find(target - nums[i]);
if(iter != map.end()) {
return {iter->second, i};
}
// 如果没找到匹配对,就把访问过的元素和下标加入到map中
// map[nums[i]]= i;
map.insert(pair<int, int>(nums[i], i));
}
return {};
}
};2.四数相加||
给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0。
为了使问题简单化,所有的 A, B, C, D 具有相同的长度 N,且 0 ≤ N ≤ 500 。所有整数的范围在 -2^28 到 2^28 - 1 之间,最终结果不会超过 2^31 - 1 。
例如:
输入:
- A = [ 1, 2]
- B = [-2,-1]
- C = [-1, 2]
- D = [ 0, 2]
输出:
2
解释:
两个元组如下:
- (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
- (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
思路
本题咋眼一看好像和0015.三数之和 (opens new window),0018.四数之和 (opens new window)差不多,其实差很多。
本题是使用哈希法的经典题目,而三数之和,四数之和并不合适使用哈希法,因为三数之和和四数之和这两道题目使用哈希法在不超时的情况下做到对结果去重是很困难的,很有多细节需要处理。
而这道题目是四个独立的数组,只要找到A[i] + B[j] + C[k] + D[l] = 0就可以,不用考虑有重复的四个元素相加等于0的情况,所以相对于题目18. 四数之和,题目15.三数之和,还是简单了不少!
如果本题想难度升级:就是给出一个数组(而不是四个数组),在这里找出四个元素相加等于0,
本题解题步骤:
- 首先定义 一个unordered_map,key放a和b两数之和,value 放a和b中两数之和出现的次数。(PS:小写字母为大写字母数组中的对应元素)
- 遍历大A和大B数组,统计两个数组元素之和,和出现的次数,放到map中。
- 定义int变量count,用来统计 a+b+c+d = 0 出现的次数。
- 在遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就用count把map中key对应的value也就是出现次数统计出来。
- 最后返回统计值 count 就可以了
class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int, int> m; //key:a+b的数值,value:a+b数值出现的次数
int count = 0; // 统计a+b+c+d = 0 出现的次数
// 遍历大A和大B数组,统计两个数组元素之和,和出现的次数,放到m中
for (int a : A) {
for (int b : B) {
m[a + b]++;
}
}
// 在遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就把map中key对应的value也就是出现次数统计出来。
for (int c : C){
for (int d : D){
if(m.count(-(c+d)))
count = count + m[-(c+d)];// 这里不是简单的count++,因为可能出现-(a+b)值一样但不同的下标组合
}
}
return count;
}
};边栏推荐
猜你喜欢

拒绝采样小记

MySQL 入门:Case 语句很好用

人脸识别AdaFace学习笔记

Wangeditor rich text editor to upload pictures and solve cross-domain problems

Cholesterol-PEG-Thiol CLS-PEG-SH Cholesterol-Polyethylene Glycol-Sulfhydryl

Cholesterol-PEG-Amine CLS-PEG-NH2 Cholesterol-Polyethylene Glycol-Amino Research Use

DSPE-PEG-COOH CAS:1403744-37-5 磷脂-聚乙二醇-羧基脂质PEG共轭物

【解决问题】RuntimeError: The size of tensor a (80) must match the size of tensor b (56) at non-singleton

Rejection sampling note

Pytorch Daily Practice - Predicting Surviving Passengers on the Titanic
随机推荐
Three methods of accessing image pixels in opencv
IDEA overview and installation and debugging
Log jar package conflict, and its solution
Four common ways of POST to submit data
MySQL master-slave switching steps
深度学习知识点杂谈
a:自我介绍
人脸识别AdaFace学习笔记
Remote file xxx is mapped to the local path xxx and can't be found. You can continue debugging....
学习JDBC之获取数据库连接的方式
Tensorflow——demo
ImportError: cannot import name 'Xxxx' from partially initialized module 'xx.xx.xx'
MW: 3400 4-Arm PEG-DSPE four-arm-polyethylene glycol-phospholipid a saturated 18-carbon phospholipid
Tensorflow相关list
实现离线文件推流成rtsp 2
这些数组技巧,我爱了
Pytorch学习笔记7——处理多维特征的输入
IDEA概述和安装及调试
Introduction to CLS-PEG-FITC Fluorescein-PEG-CLS Cholesterol-PEG-Fluorescein
Session和Cookie,Token