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.

Notice

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.

 
Example

For example, given array S = {-1 0 1 2 -1 -4}, A solution set is:

  1. (-1, 0, 1)
  2. (-1, -1, 2)

题意

给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组。

在三元组(a, b, c),要求a <= b <= c。结果不能包含重复的三元组。

解法一:

  1. class Solution {
  2. public:
  3. /**
  4. * @param numbers : Give an array numbers of n integer
  5. * @return : Find all unique triplets in the array which gives the sum of zero.
  6. */
  7. vector<vector<int> > threeSum(vector<int> &nums) {
  8. vector<vector<int> > result;
  9.  
  10. sort(nums.begin(), nums.end());
  11. for (int i = ; i < nums.size(); i++) {
  12. if (i > && nums[i] == nums[i - ]) {
  13. continue;
  14. }
  15.  
  16. // two sum;
  17. int start = i + , end = nums.size() - ;
  18. int target = -nums[i];
  19. while (start < end) {
  20. if (start > i + && nums[start - ] == nums[start]) {
  21. start++;
  22. continue;
  23. }
  24.  
  25. if (nums[start] + nums[end] < target) {
  26. start++;
  27. } else if (nums[start] + nums[end] > target) {
  28. end--;
  29. } else {
  30. vector<int> triple;
  31. triple.push_back(nums[i]);
  32. triple.push_back(nums[start]);
  33. triple.push_back(nums[end]);
  34. result.push_back(triple);
  35. start++;
  36. end--;
  37. }
  38. }
  39. }
  40.  
  41. return result;
  42. }
  43. };

解法二:

  1. class Solution {
  2. public:
  3. /*
  4. * @param numbers: Give an array numbers of n integer
  5. * @return: Find all unique triplets in the array which gives the sum of zero.
  6. */
  7. vector<vector<int>> threeSum(vector<int> &numbers) {
  8. vector<vector<int>> ret;
  9.  
  10. int n = numbers.size();
  11. sort(numbers.begin(), numbers.end());
  12.  
  13. for (int i = ; i < n - ; ++i) {
  14. if (i != && numbers[i] == numbers[i-]) {
  15. continue;
  16. }
  17.  
  18. int sum = -numbers[i];
  19. int j = i + , k = n - ;
  20.  
  21. while (j < k) {
  22. int tmp = numbers[j] + numbers[k];
  23. if (tmp == sum) {
  24. vector<int> sol{numbers[i], numbers[j], numbers[k]};
  25. ret.push_back(sol);
  26. while (j < k && numbers[j] == numbers[j+]) {
  27. j++;
  28. }
  29. while (j < k && numbers[k] == numbers[k-]) {
  30. k--;
  31. }
  32. j++;
  33. k--;
  34. } else if (tmp > sum) {
  35. k--;
  36. } else {
  37. j++;
  38. }
  39. }
  40. }
  41. return ret;
  42. }
  43. };

57. 3Sum【medium】的更多相关文章

  1. 2. Add Two Numbers【medium】

    2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...

  2. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  3. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  4. 61. Search for a Range【medium】

    61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...

  5. 62. Search in Rotated Sorted Array【medium】

    62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...

  6. 74. First Bad Version 【medium】

    74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...

  7. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

  8. 159. Find Minimum in Rotated Sorted Array 【medium】

    159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...

  9. Java for LeetCode 207 Course Schedule【Medium】

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

随机推荐

  1. Java从零开始学八(循环结构)

    一.循环结构 循环结构则是根据判断条件的成立与否,决定程序段落的执行次数,而这个程序段落就称为循环主体.

  2. 3DMax脚本插件--改动材质&amp;贴图名称

    从网上淘到了一套人物的模型,当时的心情是激动无比,掏出用的不熟练的3DMax折腾了半天.突然发现了一个蛋疼的事儿,所有的模型文件,材质名称,子材质,以及贴图所实用的是中文命名!! ! 尽管说是能跑,只 ...

  3. 设置python 命令行交互程序自己主动补齐

    1. 新建Python环境变量配置文件: vim ~/.pystartup # Add auto-completion and a stored history file of commands to ...

  4. Spring Web MVC 原理学习(下)

             接着上一篇博客,这一篇.我们依据一个简单的demo,来对SpringMVC的原理再次学习:   一.配置web.xml                   我们新建一个web项目.在 ...

  5. Java 基础【13】 I/O流概念分析整理

    转载地址:http://blog.csdn.net/yuebinghaoyuan/article/details/7388059 java.io 中的流,可以从不同的角度进行分类. 按照数据流的方向不 ...

  6. eclipse could not create the Java Vitual Machine

      eclipse could not create the Java Vitual Machine CreateTime--2018年4月27日11:07:15 Author:Marydon 1.情 ...

  7. 网速变慢解决方法.Tracert与PathPing(转)

    Tracert命令与PathPing命令你常用吗: 前段时间本网吧网速不太正常.每晚8点后到11点之间网速爆慢.其余时间则正常.在8~11点间PING电信DNS TIME值要100多MS以上,但PIN ...

  8. CSS3+JS 实现的便签应用

    概述 利用HTML5新增的 locationStorage 实现的便签应用,没有使用 JQuery,主要是为了练习原生JS的使用,采用响应式开发,在手机端和桌面端都有良好的体验,而且使用CSS3添加了 ...

  9. Android 一个apk多个ICON执行入口

    一个工程对应一个AndroidManifest.xml文件,这个文件中包含有该项目的一些设置,如权限.SDk版Activity.Service信息等.一般而言,这个文件中会有且仅有一个applicat ...

  10. 【laravel5.4】迁移文件的生成、修改、删除

    建议直接去官方文档查看: https://laravel-china.org/docs/laravel/5.4/migrations#creating-columns 1.生成迁移: 主要方式:1.创 ...