当前位置:网站首页>力扣每日一题-第47天-15. 三数之和
力扣每日一题-第47天-15. 三数之和
2022-08-04 05:44:00 【重邮研究森】
2022.8.3今天你刷题了吗?
题目:
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。
分析:
给定一个数组,我们需要找到三个数加起来为0的三个数,并且最后返回时不能有重复。那么对于这个问题,思路是:
首先利用一个二维数组来存放满足加起来=0的数,找到了这些数之和,对二维数组进行清除重复元素操作,最终得到我们的结果。在找元素时,利用暴力法,依次遍历数组,找到满足的所有元素,对于重复元素去除,可以利用文中那个简单的两行代码完成。
解析:
1.暴力法
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int tmp = 0;
int temp = 0;
int flag = 0;
int x = 0;
vector<int>vec1;
vector<vector<int>>vec;
if (nums.size() == 0)
{
return vec;
}
for (int i = 0; i < nums.size()-2; i++)
{
for (int j = i+1; j < nums.size(); j++)
{
tmp = 0 - nums[i] - nums[j];
temp = nums[j];
x = j;
while (x < nums.size())
{
if (x < nums.size()-1&&tmp == nums[x+1])
{
vec1.push_back(tmp);
vec1.push_back(temp);
vec1.push_back(nums[i]);
sort(vec1.begin(), vec1.end());
vec.push_back(vec1);
vec1.clear();
break;
}
x++;
}
}
}
//清楚重复元素
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
return vec;
}
};2.指针法
这是官方的双指针
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int n = nums.size();
sort(nums.begin(), nums.end());
vector<vector<int>> ans;
// 枚举 a
for (int first = 0; first < n; ++first) {
// 需要和上一次枚举的数不相同
if (first > 0 && nums[first] == nums[first - 1]) {
continue;
}
// c 对应的指针初始指向数组的最右端
int third = n - 1;
int target = -nums[first];
// 枚举 b
for (int second = first + 1; second < n; ++second) {
// 需要和上一次枚举的数不相同
if (second > first + 1 && nums[second] == nums[second - 1]) {
continue;
}
// 需要保证 b 的指针在 c 的指针的左侧
while (second < third && nums[second] + nums[third] > target) {
--third;
}
// 如果指针重合,随着 b 后续的增加
// 就不会有满足 a+b+c=0 并且 b<c 的 c 了,可以退出循环
if (second == third) {
break;
}
if (nums[second] + nums[third] == target) {
ans.push_back({nums[first], nums[second], nums[third]});
}
}
}
return ans;
}
};
边栏推荐
猜你喜欢

Computer software: recommend a disk space analysis tool - WizTree

ThreadLocal内存泄漏问题讲解

Database knowledge: SQLServer creates non-sa user notes

电脑知识:台式电脑应该选择品牌和组装,值得收藏

E-R图总结规范

Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv

Time Series Forecasting Based on Reptile Search RSA Optimized LSTM

IDEA中创建编写JSP

基于爬行动物搜索RSA优化LSTM的时间序列预测

Network skills: teach you to install batteries on the router, you can still surf the Internet when the power is cut off!
随机推荐
Computer knowledge: desktop computers should choose the brand and assembly, worthy of collection
MySQL内存淘汰策略
golang rtsp拉流测试
为什么不使用VS管理QT项目
90多款matlab工具箱打包放送
数据库文档生成工具V1.0
MySQL重置root密码
DenseNet详解及Keras复现代码
【C# - 爬虫】使用Selenium实现爬虫,获取近七天天气信息(包含完整代码)
微软电脑管家2.0公测版体验
Computer software: recommend a disk space analysis tool - WizTree
类图规范总结
MySQL(4)
Database knowledge: SQLServer creates non-sa user notes
手把手教你Charles抓包工具使用
【音视频开发系列】fdk_aac 之 PCM 转 AAC
mysql月份比較是否相等
七夕专属程序员的浪漫
IDEA 控制台 中文乱码问题(如果网上教程都无法解决你的问题的话)
系统流量预估、架构设计方案