LeetCode —— 3SUM
来源:互联网 发布:hdp直播软件下载 编辑:程序博客网 时间:2024/06/04 18:01
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[n]; 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[n]; if (temp < target) m++; else if (temp > target) n--; else { vec.push_back(array[m]); vec.push_back(array[n]); 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[n] == 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— 3Sum
- Sum—LeetCode-15 3Sum
- LeetCode —— 3SUM
- LeetCode——3Sum
- 3Sum——leetcode
- 3Sum —— Leetcode
- LeetCode — Two Sum
- LeetCode—Two Sum
- LeetCode—Two Sum
- LeetCode—Target Sum
- leetcode—Two Sum
- LeetCode —-Two Sum
- LeetCode——3Sum & 3Sum Closest
- LeetCode——3Sum Closest
- LeetCode——3Sum Closest
- leetcode 015 —— 3Sum
- leetcode 016 —— 3Sum Closest
- LeetCode题解——3Sum
- 如何安装CDT到Eclipse<转>
- mysql数据库数据出现诡异的127
- 这是网友发的一道很常见的面试题, for(foo('A');foo('B')&&(i<2);foo('C')) ,其实考的就是for (初始化语句; 判断条件; 循环条件) ,现在详细解析一下!
- 下一个序列
- Embedding Python in Objective-C: Part 2
- LeetCode —— 3SUM
- ERROR 2003:Can't connect to MySQL server on 'localhost' (10061)
- Git和Repo管理使用简要介绍
- Android学习笔记
- 【km笔记】CSharp--类的属性和字段
- 崩溃分析
- 关于Android线程的几点说明
- Determining and Monitoring the Connectivity Status 根据网络连接状况去省电
- 一些注意事项