首页 > 编程语言 >LeetCode 两数之和,三数之和,最接近的三数之和,四数之和(C++)

LeetCode 两数之和,三数之和,最接近的三数之和,四数之和(C++)

时间:2022-12-19 19:37:21浏览次数:42  
标签:四数 target third nums int 三数 second 两数 first


1. 两数之和

问题描述

给定一个整数数组 ​​nums​​​ 和一个目标值 ​​target​​,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。

解题思路

使用map来存储容器中的元素和数组下标,判断​​target - nums[i]​​​是否在map中,如果不在,返回​​{}​​​,如果在判断元素​​nums[i]​​​数组下标的和​​nums[target - nums[i]]​​的数组下标是否一致,只有在不一致时才符合题意。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

代码实现

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
//数组元素个数多于3个,数组中没有重复的数字
// 数组只有两个数,数组中可能有重复的数字
map<int, int> iimap;
int j = 0;
for(auto& i : nums)
iimap[i] = j++;
for(int i = 0; i < nums.size(); i++){
if(iimap.find(target - nums[i]) != iimap.end() and iimap[target - nums[i]] != i){
return {i, iimap[target - nums[i]]};
}
}
return {};
}
};

LeetCode 两数之和,三数之和,最接近的三数之和,四数之和(C++)_代码实现

2. 三数之和

问题描述

给你一个包含 ​​n​​​ 个整数的数组 ​​nums​​​,判断​​nums​​​ 中是否存在三个元素 ​​a,b,c​​​,使得 ​​a + b + c = 0​​?请你找出所有满足条件且不重复的三元组。

注意: 答案中不可以包含重复的三元组。

示例:

给定数组 nums = [-1, 0, 1, 2, -1, -4],

满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]

解题思路

  • 为避免出现重复读取,可将数组排序后再找符合题意的三元组。出现连续相等的元素,直接跳过。
  • 使用双指针的方法,从左只有每次固定一个元素,再在其元素后用两个指针寻找两个元素和为其固定元素负值即可满足题意。
  • 双指针的开始位置是:左边指针指向固定元素的后一个位置;右指针指向元素最右边。如果两个指针指向的元素和大于固定值的负值,右指针减一;小于固定值的负值,左指针加一;直到相等,压入二维数组中。

代码实现

class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int n = nums.size();
sort(nums.begin(), nums.end());
vector<vector<int>> ans;
for(int first = 0; first < n; first++){
if(first > 0 and nums[first] == nums[first - 1])
continue;
int third = n - 1; //将此行代码放到下面的for循环里面 就会超时
int target = -nums[first];
for(int second = first + 1; second < n; second++){
if(second > first + 1 and nums[second] == nums[second - 1])
continue;
while(second < third and nums[second] + nums[third] > target){
third--;
}
if(second == third)
break;
if(nums[second] + nums[third] == target){
ans.push_back({nums[first], nums[second], nums[third]});
}
}
}
return ans;
}
};

运行截图

LeetCode 两数之和,三数之和,最接近的三数之和,四数之和(C++)_代码实现_02

3、最接近的三数之和

问题描述

给定一个包括​​n​​​个整数的数组 ​​nums​​​和 一个目标值​​target​​​。找出 ​​nums​​​ 中的三个整数,使得它们的和与 ​​target​​最接近。返回这三个数的和。假定每组输入只存在唯一答案。

示例:

输入:nums = [-1,2,1,-4], target = 1
输出:2
解释:与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。

提示:

3 <= nums.length <= 10^3
-10^3 <= nums[i] <= 10^3
-10^4 <= target <= 10^4

解题思路

使用上一题“三个数之和为零”的双指针法。将数组中元素排序,从左到右每次固定一个元素,再计算包括其元素与其后面的3个元素和。三元素和与target值相比较,最后返回其绝对值最小的三元组的和。

代码实现

class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
if(nums.size() == 3)
return accumulate(nums.begin(), nums.end(), 0);
int ans = 0;
int n = nums.size();
sort(nums.begin(), nums.end());
int minnum = INT_MAX, tmp = 0;
for(int first = 0; first < n; first++){
int second = first + 1, third = n - 1;
while(second < third){
tmp = nums[first] + nums[second] + nums[third];
if(tmp == target)
return target;
if(abs(tmp - target) < minnum){
minnum = abs(tmp - target);
ans = tmp;
}
if(tmp > target)
third--;
else
second++;
}
}
return ans;
}
};

运行截图

4、四数之和

问题描述

给定一个包含​​n​​​个整数的数组​​nums​​​和一个目标值 ​​target​​​,判断​​nums​​​中是否存在四个元素​​a,b,c​​​ 和​​d​​​ ,使得​​a + b + c + d​​​ 的值与​​target​​相等?找出所有满足条件且不重复的四元组。

注意:

答案中不可以包含重复的四元组。

示例:

给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。

满足要求的四元组集合为:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]

解题思路

首先将序列从小到大排序,依次从左只有固定每一个元素,其余的元素按照求三数之和的方法即可。

代码实现

class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
int n = nums.size();
vector<vector<int> > ans;
sort(nums.begin(), nums.end());
for(int first = 0; first < n; first++){
if(first > 0 and nums[first] == nums[first - 1])
continue;
for(int second = first + 1; second < n; second++){
int tar = target - (nums[first] + nums[second]);
if(second > first + 1 and nums[second] == nums[second - 1])
continue;
int fourth = n - 1;
for(int third = second + 1; third < fourth; third++){
if(third > second + 1 and nums[third] == nums[third - 1])
continue;
while(third < fourth and nums[third] + nums[fourth] > tar)
fourth--;
if(third == fourth)
break;
if(nums[third] + nums[fourth] == tar)
ans.push_back({nums[first], nums[second], nums[third], nums[fourth]});
}
}
}
return ans;
}
};

运行截图

LeetCode 两数之和,三数之和,最接近的三数之和,四数之和(C++)_代码实现_03


标签:四数,target,third,nums,int,三数,second,两数,first
From: https://blog.51cto.com/u_15917702/5953714

相关文章

  • [LeetCode]002-两数相加
    >>>传送门题目给你两个 非空的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。请你将两个数相加,并以相同形式......
  • 力扣---15. 三数之和
    给你一个整数数组nums,判断是否存在三元组[nums[i],nums[j],nums[k]]满足i!=j、i!=k且j!=k,同时还满足nums[i]+nums[j]+nums[k]==0。请你返回所有和......
  • 代码随想录训练营第六天|LeetCode242有效的字母异位词、LeetCode349两个数组的交集、L
    LeetCode242有效的字母异位词tag:#哈希表#数组leetcode地址:242. 有效的字母异位词代码://通过数组的方式对每个字母进行统计数量,然后遍历数组,查看是否每一项都为0f......
  • [LeetCode]001-两数之和
    >>>传送门题目给定一个整数数组nums 和一个整数目标值target,请你在该数组中找出和为目标值target 的那 两个 整数,并返回它们的数组下标。你可以假设每种输入......
  • 力扣刷题(1)---两数相加
    题目:输入:nums=[2,7,11,15],target=9输出:[0,1]解释:因为nums[0]+nums[1]==9,返回[0,1]。暴力枚举publicstaticint[]twoSum(int[]nums,inttarget)......
  • 1. 两数之和
    给定一个整数数组nums 和一个整数目标值target,请你在该数组中找出和为目标值target 的那 两个 整数,并返回它们的数组下标。你可以假设每种输入只会对应一个答案......
  • 算法练习:两指针之三数之和为0
    问题描述给出一个整型数组,找出所有三个元素的组合,其组合之和等于0。要求在结果集里不含有重复的组合。举例:输入{-2, 1, -1, 2, 1}输出{-2, 1, 1 } 问题分析最容易想到的是......
  • 算法练习:两数之和
    题目:给定一个整型数组,是否能找出两个数使其和为指定的某个值?注:整型数组中不存在相同的数。一、解题方法1、暴力解法(时间复杂度O(n^2) )这是最容易想到的一种方法,即使用两......
  • 快速找出数组中两数的和
    原文:https://blog.csdn.net/weixin_30591551/article/details/94977311能否快速找出一个数组中的两个数字,让这两个数字之和等于一个给定的值,为了简化起见,我们假设这个数组......
  • 力扣18 四数之和
    题目:给你一个由n个整数组成的数组 nums,和一个目标值target。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a],nums[b],nums[c],nums[d]] (若两个四......