1. class Solution {
  2. public:
  3. void quick_order(vector<int>& num, int star, int en)//快排
  4. {
  5. int start = star;
  6. int end = en;
  7. if (start >= end)
  8. return;
  9.  
  10. int index = num[start];//就第一个设为阈值
  11. while (start < end)
  12. {
  13. while (start < end && index <= num[end])//先看右边,把第一个小于index数找出
  14. end--;
  15.  
  16. int temp1 = num[start];//交换
  17. num[start] = num[end];
  18. num[end] = temp1;
  19.  
  20. while (start < end && index >= num[start])//再看右边,把第一个大于index的数找出
  21. start++;
  22.  
  23. int temp2 = num[start];//交换
  24. num[start] = num[end];
  25. num[end] = temp2;
  26. }
  27.  
  28. quick_order(num, star, start-);//分左右子集迭代
  29. quick_order(num, start + , en);
  30. return;
  31. }
  32.  
  33. vector<vector<int>> threeSum(vector<int>& nums) {
  34. int len=nums.size();
  35. vector<vector<int>> res;
  36.  
  37. if(len==)//输入判断
  38. return res;
  39.  
  40. int start=,end=len-;
  41. quick_order(nums,start,end);//快排
  42.  
  43. vector<vector<int>> v2;
  44. for (int c = nums.size()-; c >= ; ){
  45. for (int a = , b = c-; a < b; ){
  46. int tmp_sum = nums[a]+nums[b];
  47. if (tmp_sum < -nums[c]){
  48. ++a;
  49. } else if (tmp_sum > -nums[c]){
  50. --b;
  51. } else {
  52. vector<int> v = {nums[a], nums[b], nums[c]};
  53. v2.push_back(v);
  54. do{//去重复 a b
  55. ++a;
  56. } while (a < b && nums[a-] == nums[a]);
  57. do{
  58. --b;
  59. } while (a < b && nums[b+] == nums[b]);
  60. }
  61. }
  62. do{//去重复 c
  63. --c;
  64. } while (c >= && nums[c+] == nums[c]);
  65. }
  66. return v2;
  67.  
  68. /*for(int fir=1;fir<len-1;fir++)
  69. {
  70. int sec=fir-1,thr=fir+1;
  71. while(sec>=0 && thr<=len-1)
  72. {
  73. int sum=nums[fir]+nums[sec]+nums[thr];
  74. if (sum==0)
  75. {
  76. vector<int> node={nums[sec],nums[fir],nums[thr]};
  77. bool flag=true;
  78. for(int i=0;i<res.size();i++)
  79. {
  80. if(node==res[i])
  81. flag=!flag;
  82. }
  83. if(flag)
  84. res.push_back(node);
  85. sec = sec - 1;
  86. }
  87. else if (sum>0)
  88. sec=sec-1;
  89. else if (sum<0)
  90. thr=thr+1;
  91. }
  92. }
  93.  
  94. return res;*/
  95. }
  96.  
  97. };

分析:

不写代码永远不知道自己有多菜,一个快排写的我哭了,然后说主要思想:

未注释的是别人的代码,排序后,令【a,。。。。,b,c】这种形式,让c从右往左遍历,a初始化为0,从左向右移动,b初始化c-1,从右往左移动。在c遍历中,计算a+b,若a+b<-c,说明a太小了,a++,若a+b>-c,说明b大了,b--。最让我值得学习的是去重复,其实不难,就是a,b,c在各自道路上有重复就前进。

注释的是我自己的想法,排序后,令【。。。a,b,c,。。。】这种形式,b从左向右遍历,a初始化b-1,从b往左移动,c初始化b+1,从b往右移动。在b遍历中,计算a+c,若a+b+c>0,a--,若a+b+c<0,b++。这里已经实现了除去重复所有功能,且时间复杂度和上面一样,但是就在去重复这里我懵了,我先按查找已被选择的三数,但是碰到一个变态长的数组,时间复杂度蹭蹭的上涨,然后我就分析重复情况,有两个例子【-1,0,0,1】和【-2,-1,-1,0,1,2】,一个是【【-1,0,1】】,一个是【【-1,-1,2】,【-1,0,1】】,这里面如果你让b跳过重复的,那就错过后面案例的【-1,-1,2】,如果不跳过,前面案例就会重复,搞的我又一点点分析,但是真的好难写,难写在于解决一个情况,但是又有新的情况,二者特矛盾,有一没二那种,后来代码越写越长,还丑,我就只能捂着脸学习别人的思想了。

下次再碰到想到中间指针时候,想想指针从后向前或从前向后的情况。

leecode第十五题(三数之和)的更多相关文章

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

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

  2. LeetCode 第15题-三数之和

    1. 题目 2.题目分析与思路 3.思路 1. 题目 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且 ...

  3. LeetCode第十八题-四数之和

    4Sum 问题简介:定n个整数和整数目标的数组nums,是否有元素a,b,c,d在nums中,使a+b+c+d=target? 举例: 给定数组 nums = [1, 0, -1, 0, -2, 2] ...

  4. 【leetcode 简单】第三十八题 两数之和 II - 输入有序数组

    给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值( ...

  5. leetcode 刷题(数组篇)15题 三数之和 (双指针)

    很有意思的一道题,值得好好思考,虽然难度只有Mid,但是个人觉得不比Hard简单 题目描述 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b ...

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

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

  7. #leetcode刷题之路16-最接近的三数之和

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

  8. leetcode第15题:三数之和

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

  9. Leetcode题库——16.最接近的三数之和

    @author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...

随机推荐

  1. 2019/3/27 wen 数组排序

  2. shell 脚本的时间差计算

    在某个时间点上增加一段时间 将时间转为时间戳,然后增加时间 [root@~]# date +%s -d '2017-05-27 12:0:0' 1495857600 [root@ ~]# new_ti ...

  3. Docker学习笔记之docker volume 容器卷的那些事(一)

    预览目录 volume 方式 相关用例 使用方式 使用 volume driver bind mount 方式 相关用例 使用方式 配置selinux标签 配置macOS的安装一致性 tmpfs 方式 ...

  4. axure rp 8.0注册码(亲测)

    今天在看一需求原型时,发现其他部门发过来是8.0版的,老的7不能用,找了个亲测可用的验证码. License:米 业成 (STUDENT)Key:nFmqBBvEqdvbiUjy8NZiyWiRSg3 ...

  5. [C++ Primer Plus] 第2章、开始学习c++

    一.程序清单2.1(代码和书略不一样) #include<iostream> using namespace std;//使用std这个命名空间,才能正确找到cin和cout,如果不使用命 ...

  6. [c/c++] programming之路(1)、编写程序打开记事本、计算器等

    一.命令行启动程序 通过命令行关闭程序:taskkill /f /im 程序名.exe 二.打开记事本.计算器 #include <stdlib.h> void main(){ syste ...

  7. topcoder srm 545 div1

    problem1 link 这个可以贪心地从前向后构造.假设当前已经的字符串为$S$,对于一个字符$c$来说,设将$c$加到$S$后得到的新串为$S^{'}$.那么如果$X+Y+Z \ge minIn ...

  8. 【定制Android系统】Android O 在ROM中添加自己的 so 库(1)——Android.mk 与 Android.bp 的区别【转】

    本文转载自: 版权声明:本文为博主原创文章,转载时请注明原作者及出处.    https://blog.csdn.net/u014248312/article/details/82020204需求:在 ...

  9. Oracle面试相关

    存储过程: https://www.cnblogs.com/taiguyiba/p/7809310.html https://www.cnblogs.com/lideng/p/3427822.html ...

  10. c# 之 System.Type.GetType()与Object.GetType()与typeof比较

    Object.GetType()与typeof的区别 //运算符,获得某一类型的 System.Type 对象. Type t = typeof(int); //方法,获取当前实例的类型. ; Con ...