题目链接

给n个数, 找出三个数相加结果为0的所有的组, 不可重复。

用双指针的思想,O(n^2)暴力的找, 注意判重复。

  1. class Solution {
  2. public:
  3. vector<vector<int>> threeSum(vector<int>& nums) {
  4. int sz = nums.size();
  5. vector <vector<int> > ans;
  6. vector <int> tmp;
  7. sort(nums.begin(), nums.end());
  8. for(int i = ; i<sz; i++) {
  9. if(i> && nums[i] == nums[i-])
  10. continue;
  11. int l = i+, r = sz-;
  12. while(l<r) {
  13. int sum = nums[i]+nums[l]+nums[r];
  14. if(sum < ) {
  15. l++;
  16. } else if (sum>) {
  17. r--;
  18. } else {
  19. tmp.push_back(nums[i]);
  20. tmp.push_back(nums[l++]);
  21. tmp.push_back(nums[r--]);
  22. sort(tmp.begin(), tmp.end());
  23. ans.push_back(tmp);
  24. tmp.clear();
  25. while(l<r&&nums[l-]==nums[l])
  26. l++;
  27. while(r>l&&nums[r] == nums[r+])
  28. r--;
  29. }
  30. }
  31. }
  32. return ans;
  33. }
  34. };

leetcode 15. 3Sum 双指针的更多相关文章

  1. LeetCode 15 3Sum [sort] <c++>

    LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...

  2. leetcode 15. 3Sum 二维vector

    传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...

  3. [LeetCode] 15. 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. LeetCode——15. 3Sum

    一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...

  5. LeetCode 15 3Sum(3个数求和为0的组合)

    题目链接 https://leetcode.com/problems/3sum/?tab=Description   Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...

  6. LeetCode 15. 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 ...

  7. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  8. leetCode 15. 3Sum (3数之和) 解题思路和方法

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

  9. leetcode 15 3sum & leetcode 18 4sum

    3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...

随机推荐

  1. 个人信用卡管理 - iOS UI原型

  2. (92) Web Crawling: How can I build a web crawler from scratch? - Quora

    (92) Web Crawling: How can I build a web crawler from scratch? - Quora How can I build a web crawler ...

  3. Jquery读取URL参数

    $(document).ready(function(){ function getQueryString(name) { var reg = new RegExp("(^|&)&q ...

  4. 史上最简单的Hibernate入门简单介绍

    事实上Hibernate本身是个独立的框架,它不须要不论什么web server或application server的支持.然而,大多数的Hibernate入门介绍都加入了非常多非Hibernate ...

  5. c 查找A字符串在B字符串中是否存在,计算出现的次数

    主要是应用了头文件<string.h>中的strstr函数 char * strstr(const char *s1, const char *s2); 查找是否存在: #include& ...

  6. .NET WIN7+IIS 7.5下URLRewriter组件伪静态设置

    原文地址:WIN7+IIS 7.5伪静态的设置 --------------------------------------------------------偶是分割线君-------------- ...

  7. //相当于深拷贝一份dataArray。这样才不会改变dataArray本身的值

    //相当于深拷贝一份dataArray.这样才不会改变dataArray本身的值 NSMutableArray* commitDataArray = [NSKeyedUnarchiver unarch ...

  8. Java如何实现对Mysql数据库的行锁

    场景如下:     用户账户有余额,当发生交易时,需要实时更新余额.这里如果发生并发问题,那么会造成用户余额和实际交易的不一致,这对公司和客户来说都是很危险的. 那么如何避免:     网上查了下,有 ...

  9. org.apache.tomcat.util.bcel.classfile.ClassFormatException: null is not a Java .class file

    org.apache.tomcat.util.bcel.classfile.ClassFormatException: null is not a Java .class file   在$TOMCA ...

  10. 在Oracle 11g中用看Oracle的共享内存段---------IPCS

    很早之前,在一次讲课了,用了命令ipcs,发现oracle的共享内段好小,如下: oracle@mydb ~]$ ipcs -a ------ Shared Memory Segments ----- ...