57. 3Sum【medium】

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, 0, 1)
(-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 = 0; i < nums.size(); i++) {
12             if (i > 0 && nums[i] == nums[i - 1]) {
13                 continue;
14             }
15             
16             // two sum;
17             int start = i + 1, end = nums.size() - 1;
18             int target = -nums[i];
19             while (start < end) {
20                 if (start > i + 1 && nums[start - 1] == 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 = 0; i < n - 2; ++i) {
14             if (i != 0 && numbers[i] == numbers[i-1]) {
15                 continue;
16             }
17             
18             int sum = -numbers[i];
19             int j = i + 1, k = n - 1;
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+1]) { 
27                         j++;
28                     }
29                     while (j < k && numbers[k] == numbers[k-1]) {
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 };
原文地址:https://www.cnblogs.com/abc-begin/p/8413775.html