[LeetCode]3Sum

3Sum

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.
    For example, given array S = {-1 0 1 2 -1 -4},

    A solution set is:
    (-1, 0, 1)
    (-1, -1, 2)

利用Hash Table可将时间复杂度降为O(n^2),原因是将第三个循环遍历改成了Hash Table的判断key是否存在,和Two Sum类似。
首先进行排序,时间复杂度n*log(n), 然后固定前两个值,查找是否存在符合条件的第三个值。
为了节约时间,已经出现的第一个数字如果再次出现可以直接continue了。
 1 class Solution {
 2 public:
 3     vector<vector<int>> threeSum(vector<int>& nums) {
 4         vector<vector<int>> result;
 5         vector<int> temp;
 6         sort(nums.begin(),nums.end());
 7         unordered_map<int,int> showed;
 8         for(int i=0;i<nums.size();i++)
 9         {
10             showed[nums[i]] = i;
11         }
12         for(int i=0;i<nums.size();i++)
13         {
14             if((i>0)&&(nums[i]==nums[i-1])) continue;
15             for(int j=i+1;j<nums.size();j++)
16             {
17                 if((j>(i+1))&&(nums[j]==nums[j-1])) continue;
18                 int num1=nums[i];
19                 int num2=nums[j];
20                 int target = 0-num1-num2;
21                 if((showed.find(target)!=showed.end())&&(showed[target]>j))
22                 {
23                     temp.push_back(num1);
24                     temp.push_back(num2);
25                     temp.push_back(target);
26                     result.push_back(temp);
27                     temp.clear();
28                 }
29             }
30         }
31         return result;
32     }
33 };

 也可以用二分查找的思路来完成。

 1 class Solution {
 2 public:
 3     vector<vector<int>> threeSum(vector<int>& nums) {
 4         vector<vector<int>> result;
 5         set<vector<int>> tempres;
 6         sort(nums.begin(),nums.end());
 7         for(int i=0;i<nums.size();i++)
 8         {
 9             if((i>0)&&(nums[i]==nums[i-1])) continue;
10             int begin = i+1;
11             int end = nums.size()-1;
12             int target = 0-nums[i];
13             while(begin<end)
14             {
15                 if((nums[begin]+nums[end])==target)
16                 {
17                     vector<int> temp;
18                     temp.push_back(nums[i]);
19                     temp.push_back(nums[begin]);
20                     temp.push_back(nums[end]);
21                     tempres.insert(temp);
22                     begin++;
23                     end--;
24                 }
25                 else if((nums[begin]+nums[end])<target)
26                 {
27                     begin++;
28                 }
29                 else
30                 {
31                     end--;
32                 }
33             }
34         }
35         set<vector<int>>::iterator it;
36         for(it=tempres.begin();it!=tempres.end();it++)
37         {
38             result.push_back(*it);
39         }
40         return result;
41     }
42 };

 

 
posted @ 2015-08-24 12:12  Sean_le  阅读(174)  评论(0编辑  收藏  举报