Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

  • Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
  • The solution set must not contain duplicate triplets.

思路:

把最小值从头到尾循环,中间值和最大值两边收缩。

我写的时候是在最后去重复,总是超时。后来看了人家的答案,发现可以每次对最小、中间、最大值去重。就可以AC了

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <stack>
  6. using namespace std;
  7.  
  8. class Solution {
  9. public:
  10. vector<vector<int> > threeSum(vector<int> &num) {
  11. vector<vector<int> > ans;
  12. if(num.size() < )
  13. {
  14. return ans;
  15. }
  16. int small = ;
  17. int big = num.size() - ;
  18. int mid = ;
  19. int sum = ;
  20. sort(num.begin(), num.end());
  21. for(small = ; small < num.size() - ; /*small++*/) //最小数字循环 中间与最大数字两边收缩
  22. {
  23. mid = small + ;
  24. big = num.size() - ;
  25. while(mid < big)
  26. {
  27. sum = num[small] + num[mid] + num[big];
  28. if(sum > )
  29. {
  30. big--;
  31. }
  32. else if(sum < )
  33. {
  34. mid++;
  35. }
  36. else
  37. {
  38. vector<int> v;
  39. v.push_back(num[small]);
  40. v.push_back(num[mid]);
  41. v.push_back(num[big]);
  42. ans.push_back(v);
  43. do { mid++; }while (mid < big && num[mid - ] == num[mid]); //注意!!
  44. do { big--; }while (mid < big && num[big + ] == num[big]);//注意!!
  45. }
  46. }
  47. do{ small++; }while (small < num.size() - && num[small - ] == num[small]);//注意!!
  48. }
  49. return ans;
  50. }
  51. };
  52.  
  53. int main()
  54. {
  55. Solution s;
  56. vector<int> num;
  57. num.push_back(-);
  58. num.push_back(-);
  59. num.push_back(-);
  60. num.push_back(-);
  61. num.push_back(-);
  62. num.push_back();
  63. num.push_back();
  64. num.push_back();
  65. num.push_back();
  66. num.push_back();
  67.  
  68. vector<vector<int>> ans = s.threeSum(num);
  69.  
  70. return ;
  71.  
  72. }

【leetcode】3Sum (medium)的更多相关文章

  1. 【LeetCode】3Sum 解决报告

    这个问题是我目前的知识回答,不来,只有良好的网上搜索解决方案,发现 K Sum 它是一类问题,但是,互联网是没有更简洁的代码,我想对于谁刚开始学习的人.您可能仍然想看看这个问题该怎么解决,然后看看他们 ...

  2. 【LeetCode】3Sum Closest 解题报告

    [题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...

  3. 【leetcode】Subsets (Medium) ☆

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  4. 【leetcode】3Sum Closest

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  5. 【leetcode】3Sum

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

  6. 【leetcode】3Sum Closest(middle)

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

  7. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

  8. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  9. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

随机推荐

  1. IOS开发之----NSDictionary,JSON和XML互相转换

    本文永久地址为 http://www.cnblogs.com/ChenYilong/p/4044521.html,转载请注明出处.     -(void)test {     //XML文本范例   ...

  2. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  3. 2015Summer Training #2

    暑假集训昨天刚开始,14级的小伙伴快到齐了,hhhhh ,毕竟下学期区域赛,对我们来说还是很困难的. 打算每天写份总结. UVA 11300 C.Spreading the Wealth 题目大意:n ...

  4. jquery mobile cannot be created in a document with origin 'null' and URL

    jquery mobile cannot be created in a document with origin 'null' and URL http://zhidao.baidu.com/lin ...

  5. 如何在CentOS 7上安装EPEL源

    EPEL 是什么? EPEL (Extra Packages for Enterprise Linux,企业版Linux的额外软件包) 是Fedora小组维护的一个软件仓库项目,为RHEL/CentO ...

  6. SQL server 语句新建用户、对用户授权、删除用户实例

    Grant select on tb to db_user --给db_user用户授权 tb表 查询权限 一.命令操作 USE mydb GO --1. 新建测试用户 --1.1 添加登录用户和密码 ...

  7. BZOJ3052——糖果公园

    0.题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3052 1.题目大意:给定一颗n个点的无根树,每个点有一个颜色,要进行q次操作,有两种操 ...

  8. python os.path模块常用方法详解:转:http://wangwei007.blog.51cto.com/68019/1104940

    1.os.path.abspath(path) 返回path规范化的绝对路径. >>> os.path.abspath('test.csv') 'C:\\Python25\\test ...

  9. linux kernel 平台总线实例分析

    linux 平台总线的实现有三大块  , platform bus , platform device , platform drvice 平台类型结构体: /** * struct bus_type ...

  10. POJ 2631 DFS+带权无向图最长路径

    http://poj.org/problem?id=2631 2333水题, 有一个小技巧是说随便找一个点作为起点, 找到这个点的最远点, 以这个最远点为起点, 再次找到的最远点就是这个图的最远点 证 ...