算法第三周 3Sum[medium]
2017-09-21 23:14
190 查看
3Sum[medium]
Description
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: 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]
]
Analysis
这道题目是之前所做的Add Two Sum的升级版,这道题目要求寻找数组中和为0的3个数,与之前所做的题目的最大区别是两个数的和题目要求解只有一个,而这道题目明显可能存在这多组解,除此之外,我们不能从题目给的例子可以看出,解得给出存在着一定的顺序,所以我们可以得出本题所需要考虑的三大要点:求和
排序
去重
一开始,我打算采用类似于Add Two Sum的算法,即多重循环;
for (int i = 0; i < s; i++) { for (int j = i + 1; j < s; j++) { int sumoftwo = nums[i]+nums[j]; for (int k = j+1; k < s; k++) { if ((sumoftwo+nums[k]) == 0) { vector<int> temp; temp.push_back(nums[i]); temp.push_back(nums[j]); temp.push_back(nums[k]); sort(temp.begin(), temp.end()); result.push_back(temp); } } } }
此题中排序主要利用的是STL中vector的sort函数;
去重利用的是erase函数;
result.erase(unique(result.begin(), result.end()), result.end());
然而不幸的是由于复杂度为O(n*n*n),导致运行超时,那还有什么方法可以降低复杂度,经过思考以及查阅资料
首先,将原数组排序;
以每一个数的相反数作为剩下两个数求和的target(类似于add two number);
设置两个变量h,r 分别从前,从后遍历数组,终止条件即为h >= r;
- 当 nums[h]+nums[r] 大于 target 要缩小其中一个 即r- -;
- 当 nums[h]+nums[r] 大于 target 要增大其中一个 即h++;
- 当 nums[h]+nums[r] 等于 target 即获得满足要求的组合;
接下来的遍历则要避免nums[h],nums[r]的重复;
Solution
9707class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> result; sort(nums.begin(), nums.end()); int s = nums.size(); for (int i = 0; i < s; i++) { if (i >= 1) { if (nums[i] == nums[i-1]) continue; } int sumoftwo = -nums[i]; int h = i+1; int r = s-1; while (h < r) { if (nums[h]+nums[r] > sumoftwo) r--; else if (nums[h]+nums[r] < sumoftwo) h++; else if (nums[h]+nums[r] == sumoftwo) { vector<int> temp; temp.push_back(nums[i]); temp.push_back(nums[h]); temp.push_back(nums[r]); result.push_back(temp); int v1 = nums[h]; int v2 = nums[r]; while (h < r&&nums[h] == v1) h++; while (h < r&&nums[r] == v2) r--; } } } return result; } };
相关文章推荐
- (算法分析Week2)3Sum[Medium]
- 第三周项目2-建设“顺序表”算法库
- 算法第九周Course Schedule[medium]
- 蜗牛慢慢爬 LeetCode 15. 3Sum [Difficulty: Medium]
- 第三周项目2-建设“顺序表”算法库
- 第三周项目2-建设“顺序表”算法库
- 第三周项目2 建设“顺序表”算法库
- 数据结构第三周项目-自建顺序表算法库
- 【第三周 项目2-“顺序表”算法库】
- 【算法】2SUM/3SUM/4SUM问题
- Medium 15题 3Sum
- 算法分析与设计——leetcode刷题之Add Two Numbers(Medium)
- 第三周项目2-建设“顺序表”算法库
- 第三周项目二—建设“顺序表”算法库
- 第三周-项目2 - 建设“顺序表”算法库
- 算法分析与设计丨第十三周丨LeetCode(17)——Coin Change(Medium)
- LeetCode-15-3Sum(证明/KSum)-Medium
- 第三周项目1-顺序表的基本算法
- hdu 1068 D - Girls and Boys【匈牙利算法】【第三周练习】【the first day】
- 算法分析与设计丨第四周丨LeetCode(8)——Binary Tree Level Order Traversal(Medium)