注意题目没要求数字只能用一次

a + b + c = 0 即为 -b=a+c,同时要求数字不全为正(然后发现a+b+c就行。。。不过多想想没坏处嘛)

先处理特殊情况,然后

排序

注意不重复,只需要有一个数不同就行

排序后

对于某组a + b + c = 0 a最小,b,c大于a(都在a的右侧),范围在index【a】-len,这样从两边逼近,时间复杂度最低(可能有多组符合情况)

然后就是遍历,从0-n,得a找bc。因为不重复,nums[I]=NUMS[I-1]就跳过.同时匹配到了b,c后,b++,c--,也要去掉相同的数

然后刚刚想到的同时要求数字不全为正,所以nums[i]>0就没必要继续了

class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>>ret;
if(nums.empty())
return ret;
int len=nums.size();
if(len<3)
return ret;
sort(nums.begin(),nums.end());
for (int i = 0; i < len - 2; i++) {
if (nums[i] > 0)
break;
if (i > 0 && nums[i] == nums[i - 1])
continue;
int l = i + 1;
int r = len - 1;
while (l < r) { //多组
int s = nums[i] + nums[l] + nums[r];
if (s > 0)
r--;
else if (s < 0)
l++;
else {
ret.push_back({nums[i], nums[l], nums[r]});
while (l < r && nums[l] == nums[++l]);
while (l < r && nums[r] == nums[--r]);
}
}
}
return ret;
}
};

leetcode15 三数之和 双指针的更多相关文章

  1. Leetcode13. 罗马数字转整数Leetcode14. 最长公共前缀Leetcode15. 三数之和Leetcode16. 最接近的三数之和Leetcode17. 电话号码的字母组合

    > 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 ![在这里插入图片描述](https://img-blog.csdnimg.cn/63802fda72be45eba98d9e4 ...

  2. 【算法训练营day7】LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和

    [算法训练营day7]LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和 LeetCode454. 四数相加I ...

  3. LeetCode15. 三数之和

    15. 三数之和 描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中 ...

  4. [Swift]LeetCode15. 三数之和 | 3Sum

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

  5. LeetCode15.三数之和 JavaScript

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

  6. leetcode16 最接近的三数之和 双指针

    三个数循环太复杂 确定一个数,搜索另两个 先排序,之后就确定了搜索的策略 if(tp>target) while (l < r && nums[r] == nums[--r ...

  7. 【LeetCode】三数之和【排序,固定一个数,然后双指针寻找另外两个数】

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

  8. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  9. [LeetCode] 3Sum 三数之和

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

随机推荐

  1. cursor pin s和cursor pin s wait on x

    1.cursor pin s是一个共享锁,一般情况下是因为发生在SQL短时间内大量执行 案例:在生产库中,突然出现大量的cursor pin s的等待,询问是否有动作后,同事说有编译存储过程(被误导了 ...

  2. STM32F207时钟系统解析

    在前几天的文章<晶振原理解析>中介绍了晶振如何产生时钟的,板子使用的是25M无源晶振,下文将介绍STM32F207的时钟系统如何将25M晶振时钟转换为120M系统主频时钟的. 01.时钟系 ...

  3. ASP.NET Core错误处理中间件[4]: 响应状态码页面

    StatusCodePagesMiddleware中间件与ExceptionHandlerMiddleware中间件类似,它们都是在后续请求处理过程中"出错"的情况下利用一个错误处 ...

  4. JAVA获取当前文件路径this.getClass().getResource方法详细讲解

    public class Test { public void run() { // TODO Auto-generated method stub System.out.println(" ...

  5. 04. struts2中Result配置的各种视图转发类型

    概述 <action name="helloworld" class="com.liuyong666.action.HelloWorldAction"&g ...

  6. 深圳某小公司面试题:AQS是什么?公平锁和非公平锁?ReentrantLock?

    AQS总体来说没有想象中那么难,只要了解它的实现框架,那理解起来就不是什么问题了. AQS在Java还是占很重要的地位的,面试也是经常会问. 目前已经连载11篇啦!进度是一周更新两篇,欢迎持续关注 [ ...

  7. error: Failed dependencies: rpm安装包失败报错依赖包

    error: Failed dependencies: mysql-community-release conflicts with (installed) mysql57-community-rel ...

  8. 墓碑机制与 iOS 应用程序的生命周期

    ① 应用程序的状态 iOS 应用程序一共有 5 种状态: Not running:应用未运行 Inactive:应用运行在 foreground 但没有接收事件 Active:应用运行在 foregr ...

  9. (转载)微软数据挖掘算法:Microsoft 聚类分析算法(2)

    介绍: Microsoft 聚类分析算法是一种"分段"或"聚类分析"算法,它遍历数据集中的事例,以将它们分组到包含相似特征的分类中. 在浏览数据.标识数据中的异 ...

  10. Redis集群拆分原则之AKF

    当我们搭建集群的时候,首先要想明白需要解决哪些问题,搞清楚这个之前,想想单节点.单实例.单机有哪些问题? 单点故障 容量有限 可支持的连接有限(性能不足) ...... 为了解决这些问题,我们需要对服 ...