题目:

http://www.leetcode.com/2010/04/finding-all-unique-triplets-that-sums.html

分析:

首先brute force,O(n3)。i j k从0到n-1各循环一遍,肯定不能要。

其次,我们先假设只有2个数,对于一个排序的数组来说,如果想要a + b = 0,那么可以两个指针,一个在head(),一个在tail(),然后判断这两个it的值相加是否等于0,如果小于,那么前面的指针++,如果大于,那么后面的指针--,直到指针相遇或者找到和为0的一对。这使得2个数字的算法复杂度由O(n2) -> O(n)。

然后,对于3个数字的相加,先变为 a + b = -c; 那么,对于其中一个数,做一个O(n)的遍历,剩下两个数进行一个算法复杂度的降低(O(n2) -> O(n))。得到如下代码:

    set<vector<int> > threeSum(vector<int> &num) {sort(num.begin(), num.end());set< vector<int> > triplets;vector<int> triplet(3);int n = num.size();for (int i = 0; i < n; i++){int j = i + 1;int k = n - 1;while (j < k){if (num[j] + num[k] < 0 - num[i])j++;else if (num[j] + num[k] > 0 - num[i])k--;else{vector<int>::iterator it = triplet.begin();*it++ = num[i];*it++ = num[j];*it++ = num[k];j++;k--;triplets.insert(triplet);}}}return triplets;}

最终的复杂度为O(n2)。

另外转载一篇从网上看来的,返回值是vector< vector<int> >的解法,非常好!在LeetCode Online Judge上不会超时:

http://www.cnblogs.com/codingmylife/archive/2012/08/30/2663796.html

它的思想是手动过滤重复。

// Dedup directly,
// LeetCode Judge Large, 272 milli secs.
vector<vector<int> > three_sum(vector<int> &num)
{vector<vector<int> > ret;if (num.size() == 0) return ret;sort(num.begin(), num.end());for (vector<int>::const_iterator it = num.begin();it != num.end();++it){// Dedupif (it != num.begin() && *it == *(it - 1)){continue;}// Dedup, front = it + 1vector<int>::const_iterator front = it + 1;vector<int>::const_iterator back = num.end() - 1;while (front < back){const int sum = *it + *front + *back;if (sum > 0){--back;}else if (sum < 0){++front;}// Dedupelse if (front != it + 1 && *front == *(front - 1)){++front;}// Dedupelse if (back != num.end() - 1 && *back == *(back + 1)){--back;}else{vector<int> result;// Already sorted.result.push_back(*it);result.push_back(*front);result.push_back(*back);ret.push_back(result);++front;--back;}}}return ret;
}

EOF

Finding all unique triplets that sums to zero[部分转载]相关推荐

  1. Using unique option prefix myisam-recover instead of myisam-recover-option

    [转载]关于mysql error.log报"Using unique option prefix myisam-recover instead of myisam-recover-opti ...

  2. C++ unique

    algorithm中的unique  用于去除指定容器中重复的元素,返回一个没有重复元素的序列的最后一个元素,要打印这样的数据信息则从容器的begin()开始到这个返回的地方就可以了. vector& ...

  3. 【重点 递归构造二叉树】LeetCode 95. Unique Binary Search Trees II

    LeetCode 95. Unique Binary Search Trees II 本博客转载自:[1]https://segmentfault.com/a/1190000007443961 [2] ...

  4. 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 ...

  5. leetcode15 3Sum 从数组中找到三个整数,它们的和为0

    题目要求 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  6. [Algorithm]九章七:Two Pointer

    604. Window Sum:点击打开链接 例如:[1,2,7,8,5], k=3 sum[0]=nums[0]+nums[1]+nums[2]=10 sum[1]=sum[0]-nums[0]+n ...

  7. LeetCode面试常见100题( TOP 100 Liked Questions)

    这篇文章是关于LeetCode Top 100 Liked Questions 的 专栏记录,其中部分题目可能包括解题思路和多种优化解法.我把自己的思路都记录在这里,如果你看见了,请记得点个赞吧,蟹蟹 ...

  8. leetcode -- 3 sum

    3-sum 题目描写叙述: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = ...

  9. LeetCode实战:三数之和

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

最新文章

  1. Linux下使用Vi是方向键变乱码 退格键不能使用的解决方法
  2. PHP 6:PHP 基本数据类型
  3. img解包工具_3、Linux基础命令篇、压缩打包工具
  4. 超微服务器电源短接启动图解_教你一招,让你的电脑启动速度秒杀别人
  5. chrome浏览器解决ajax跨域问题
  6. 谷歌AI算法 助力可控核聚变研究
  7. WinSCP断线,WinSCP总是断线,断线重连过程又需要卡很长时间解决办法
  8. 《通信技术导论(原书第5版)》——2.5 内部使用的IP专用交换系统
  9. t620服务器安装系统,请教高手Dell T620 服务器安装Ubuntu14.04LTS桌面版 非常慢 为什么?...
  10. 微信编辑器新手入门必读
  11. 高淇python讲义百度文库_Python课堂笔记(高淇400集第一季).pdf
  12. transformer中的多头注意力机制
  13. 讲解三层代码讲解(DLL规则层如何接收服务器的数据,又如何交回给服务器)--第四课(*****) DATE :2004-06-01...
  14. 两波形相位差的计算值_有功功率、无功功率和视在功率该怎么计算?
  15. 求职套题2---各大公司
  16. 电脑卡修复计算机有用吗,电脑卡重装系统有用吗|电脑太卡可以重装系统解决吗...
  17. 10个python接私活的平台,整整10个,总有适合你的,你有技术就有钱
  18. Win98 源代码(特别版)
  19. 移动互联网引发大融合与大变革
  20. vivo2020校招-软件开发类-编程题

热门文章

  1. U盘安装CentOS7的最终解决方案
  2. 如何使用C#调用REST api?
  3. 我不断收到“ Uncaught SyntaxError:意外令牌o”
  4. 如何在Visual Studio 2012中撤消“范围到此”?
  5. Laravel 5无法打开所需的bootstrap /../ vendor / autoload.php
  6. 使用XMLHttpRequest发送POST数据
  7. 是否有一个“先前的兄弟”选择器?
  8. 计算机等级ms答题演示,计算机等级一级MS Office考试考题:第六套演示文稿题
  9. win11快捷键失效怎么处理 Windows快捷键失效的解决方法
  10. win11怎样在多屏中设置主显示器 Windows11主显示器的设置方法