今天做了力扣中的一道经典题:三数之和。这题思路倒是很快想到,调逻辑可把我调得够呛,这也正说明我的刷题思维远远不够,比起我室友半个月刷300多题的思维差远了。。。革命尚未成功,同志仍需努力。

原题链接:15. 三数之和 - 力扣(LeetCode) (leetcode-cn.com)

题解:题目要我们找到所给数组中所有和为0且不重复的三元数组。

由于题目中所给的数组长度不超过3000,所以最后的算法O(n2)也是可以接受的。数据最大值为1e5,也不用开long long。

如果直接枚举所有的情况,首先在时间复杂度上就无法接受:枚举所有情况需要三重循环,时间复杂度会达到O(n3)。并且暴力枚举的情况复杂,代码量也不小,很麻烦,所以我们要从题中寻找突破口。

题中的关键点在于:nums[i]+nums[j]+nums[k]=0。假设三个元素分别为a,b,c。这就意味着a,b,c三个数不全为正或负,必然有正有负。不妨假设a<=b<=c,那么a必然为负。因此,我们就可以在小于0的范围内枚举a,在大于a的情况下枚举b和c满足a+b+c=0即可。这样可以提升算法的效率,减少不必要的搜索。

根据我们上面的思路,不难发现要想轻松实现需要将数组排序,然后在小于0的范围内根据每一个a来在大于a的范围内枚举b和c。这是b和c的情况就是两数之和的情况了,因为数组已经有序,我们可以使用双指针的方法来解决此问题。

还有最重要的一点:去重,题目中要求不重复的三元组,因此在枚举的过程中我们需要去掉重复的情况。

  1. class Solution {
  2. public:
  3. vector<vector<int>> threeSum(vector<int>& nums) {
  4. vector<vector<int>> ans;
  5. int n=nums.size();
  6. if(n<3) return {};
  7. sort(nums.begin(),nums.end());
  8. int f,s,t;
  9. for(f=0;f<n-2;f++){ //f的范围只到n-3
  10. if(nums[f]>0) return ans;
  11. if(f>0&&nums[f]==nums[f-1]) continue; //去掉a的重复元素
  12. s=f+1;
  13. t=n-1;
  14. while(s<t){
  15. if(nums[f]+nums[s]+nums[t]==0){
  16. vector<int> res={nums[f],nums[s],nums[t]};
  17. ans.emplace_back(res);
  18. s++;t--;
  19. while(s<t&&nums[s]==nums[s-1]) s++; //去掉b的重复元素
  20. while(s<t&&nums[t]==nums[t+1]) t--; //去掉c的重复元素
  21. }
  22. if(nums[f]+nums[s]+nums[t]>0){
  23. t--;
  24. }
  25. if(nums[f]+nums[s]+nums[t]<0){
  26. s++;
  27. }
  28. }
  29. }
  30. return ans;
  31. }
  32. };

时间复杂度:O(N2)

【LeetCode_15】——三数之和的更多相关文章

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

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

  2. [LeetCode] 3Sum Closest 最近三数之和

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  3. [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 ...

  4. lintcode: 三数之和II

    题目 三数之和 II 给一个包含n个整数的数组S, 找到和与给定整数target最接近的三元组,返回这三个数的和. 样例 例如S = .  和最接近1的三元组是 -1 + 2 + 1 = 2. 注意 ...

  5. lintcode:三数之和

    题目 三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 样例 如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集 ...

  6. LeetCode 16. 3Sum Closest. (最接近的三数之和)

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  7. LeeCode数组第15题三数之和

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

  8. LeetCode第十六题-找出数组中三数之和最接近目标值的答案

    3Sum Closest 问题简介: 给定n个整数的数组nums和整数目标,在nums中找到三个整数,使得总和最接近目标,返回三个整数的总和,可以假设每个输入都只有一个解决方案 举例: 给定数组:nu ...

  9. 南大算法设计与分析课程OJ答案代码(4)--变位词、三数之和

    问题 A: 变位词 时间限制: 2 Sec  内存限制: 10 MB提交: 322  解决: 59提交 状态 算法问答 题目描述 请大家在做oj题之前,仔细阅读关于抄袭的说明http://www.bi ...

  10. python三数之和

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

随机推荐

  1. class_schedule

    #!/usr/bin/python # -*- coding: UTF-8 -*- class Schedule(object):          def __init__(self, name=& ...

  2. CentOS 7 安装步骤以及初始化

    2. 虚拟机分配的资源 因为用的软件不一样,这里设置方法无法截图,但大至如下: 2CPU/1G内存/200G硬盘 去掉打印机等没用的硬件(macOS要去掉打印机和摄像头) 光盘开始选择空白光盘,不要在 ...

  3. 【git】git子模块操作-添加子模块与克隆子模块

    https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E5%AD%90%E6%A8%A1%E5%9D%97 git submodule upda ...

  4. Linux 使用Nginx部署web项目

    https://blog.csdn.net/weixin_43233914/article/details/126483734

  5. python requests 内置请求模块

  6. 使用phpexcel导出excel和phpword导出word--简单使用

    <?php namespace app\index\controller; //离线环境不能使用composer安装,只能下载包文件,然后放在vendor下,代码中require使用 requi ...

  7. json extionsion

    using System.Collections.Generic;using Newtonsoft.Json;using Newtonsoft.Json.Converters; namespace D ...

  8. 工程师突击:SAP ABAP实用程序开发攻略.pdf

    工程师突击:SAP ABAP实用程序开发攻略.pdf 有需要的联系 wx :erpworld

  9. unity 调试 packages

    package中代码vs无法f12跳转 解决方法 1 把包copy出来 2 Package Manager->Add package from disk 3 选择包文件中的package.jso ...

  10. Onur Mutlu 18-447 Lecture9 分支预测-1

    =============== 第一部分:branch prediction =========== 1. 最简单的分支预测:总是预测下一条指令的地址在 PC+4 如何让这种分支预测更加有效呢? Id ...