LeetCode —— 3SUM
2013-04-20 15:21
295 查看
3Sum
链接:http://leetcode.com/onlinejudge#question_15原题:
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:
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)
思路:DFS + 剪枝
暴力搜索,复杂度是O(n^3),可以加剪枝
先对数组排序
1)如果 sum + array[i] > 0 那么就不用递归下去了
2)如果 i > start, 并且 array[i] == array[i-1],就不用递归了,这样还可以避免重复到组合进入最终结果里
做完之后,搜了一下其他人到做法,发现有O(n^2)到算法,还是蛮巧妙到,大概到思路是
先排序,然后对第一个数进行0到size-3遍历,后两个数是两头往中间走。具体可以参考:
http://www.cnblogs.com/etcow/articles/2547108.html
代码:
class Solution { public: vector<vector<int> > threeSum(vector<int> &num) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<int> vec; vector<vector<int> > collection; sort(num.begin(), num.end(), less<int>() ); search(0, 0, 3, num, vec, collection); return collection; } private: void search(int start, int sum, int count, const vector<int> &array, vector<int> &vec, vector<vector<int> > &collection) { if (count == 0) { if (sum == 0) collection.push_back(vec); return; } int size = array.size() - count; for (int i=start; i<=size; i++) { if (i > start && array[i-1] == array[i]) continue; if (sum + array[i] > 0) return; vec.push_back(array[i]); search(i+1, sum+array[i], count-1, array, vec, collection); vec.pop_back(); } } };
3Sum Closest
链接:http://leetcode.com/onlinejudge#question_16原题:
Given an array S of n integers,
find three integers in S such
that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
思路:如果将三个数的和减去target,那么就是求最接近0到组合了,就可以归结为3sum的问题了。
这回尝试用遍历第一个数,然后后两个数从两端往中间走的策略。复杂度为O(n^2),当然先排序。
代码:
class Solution { public: int threeSumClosest(vector<int> &num, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function if (num.size() <= 3) { int sum = 0; for (int i=0; i<num.size(); i++) sum += num[i]; return sum; } int min = num[0] + num[1] + num[2] - target; sort(num.begin(), num.end(), less<int>() ); for (int i=0; i<num.size()-2; i++) { if (i>0 && num[i] == num[i-1]) continue; int part = num[i] - target; for (int m=i+1, n=num.size()-1; m<n; ) { int sum = part + num[m] + num ; if (abs(sum) < abs(min)) min = sum; if (sum > 0) n--; else if (sum < 0) m++; else return target; } } return target + min; } };
4Sum
链接:http://leetcode.com/onlinejudge#question_18原题:
Given an array S of n integers,
are there elements a, b, c,
and d in S such
that a + b + c + d =
target? Find all unique quadruplets in the array which gives the sum of target.
Note:
Elements in a quadruplet (a,b,c,d)
must be in non-descending order. (ie, a ? b ? c ? d)
The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0. A solution set is: (-1, 0, 0, 1) (-2, -1, 1, 2) (-2, 0, 0, 2)
思路:直接用 DFS + 剪枝会超时,所以采用一二两个数遍历,三四两个数两头向中间靠拢,
要小心重复组合的加入。
代码:
class Solution { public: vector<vector<int> > fourSum(vector<int> &num, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<int> vec; vector<vector<int> > collection; if (num.size() < 4) return collection; sort(num.begin(), num.end(), less<int>() ); search(0, 0, 4, target, num, vec, collection); return collection; } private: void search(int start, int sum, int count, int target, const vector<int> &array, vector<int> &vec, vector<vector<int> > &collection) { if (count == 2) { for (int m=start, n=array.size()-1; m<n;) { int temp = sum + array[m] + array ; if (temp < target) m++; else if (temp > target) n--; else { vec.push_back(array[m]); vec.push_back(array ); collection.push_back(vec); vec.pop_back(); vec.pop_back(); m++; n--; while (m < array.size() && array[m] == array[m-1]) m++; while (n >= 0 && array == array[n+1]) n--; } } return; } int size = array.size() - count; for (int i=start; i<=size; i++) { if (i > start && array[i-1] == array[i]) continue; vec.push_back(array[i]); search(i+1, sum+array[i], count-1, target, array, vec, collection); vec.pop_back(); } } };
相关文章推荐
- leetcode[15]3Sum
- leetcode 16 -- 3Sum Closest
- LeetCode 16 3Sum Closest
- 算法分析与设计——LeetCode:16. 3Sum Closest
- 【LeetCode014-015算法/编程练习C++】最长共同前缀,3Sum(和为0) //用到了map的自动排序
- 【LeetCode】3Sum,3Sum Closest 题解报告
- Leetcode || 3Sum
- LeetCode15. 3Sum
- Leetcode-15: 3Sum
- LeetCode 15. 3Sum
- LeetCode No.15 3Sum
- 【LeetCode 15】3Sum (C++)
- leetcode—3sum
- 【leetcode-16】3Sum Closest(java)
- LeetCode-15 3Sum(求3数和为零的情况总数)
- LeetCode 016 3Sum Closest
- [LeetCode] 15. 3Sum 三数之和 @python
- LeetCode 15 -- 3Sum
- leetcode15. 3Sum
- [Leetcode]3Sum