LeetCode#15* 3Sum && LeetCode#16 3Sum Closest && LeetCode#18 4Sum
2017-07-17 20:37
543 查看
【3Sum】
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]
]
题意:给定n个整数的数组S,是否在 数组S中有元素a,b,C,使得A + B + C =0?在数组中找出独一无二的三元素组,使得他们之和为0。
注意:
在三元素组(A,B,C)中,必须满足非递减排序。 (即A≤B≤C)
该解决方案集中一定不能包含重复的三元素组。
思路:先对数组进行排序,然后枚举第一个数的位置,查找另外两个数的和等于-nums[i]的组合,由于数组排好序了,所以可以从两边往中间走,当结果大于0的时候后边往后退一步,否则前边进一步,时间复杂度O(n^2),所以时间复杂度为O(n^2)。
【3Sum Closet】
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的值距离最短的那个和。
思路:此题与上面那个题基本类似,甚至更简单一些,只需要比较和的结果即可,碰到和等于target的时候就直接返回吧!!!
【4Sum】
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: 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]
]
题意:给一个数组,和一个target整数,求数组中能够使得和为target的所有组合a,b,c,d并满足a<=b<=c<=d。
思路:与上面的3Sum很相似,只不过多了一重循环。
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]
]
题意:给定n个整数的数组S,是否在 数组S中有元素a,b,C,使得A + B + C =0?在数组中找出独一无二的三元素组,使得他们之和为0。
注意:
在三元素组(A,B,C)中,必须满足非递减排序。 (即A≤B≤C)
该解决方案集中一定不能包含重复的三元素组。
思路:先对数组进行排序,然后枚举第一个数的位置,查找另外两个数的和等于-nums[i]的组合,由于数组排好序了,所以可以从两边往中间走,当结果大于0的时候后边往后退一步,否则前边进一步,时间复杂度O(n^2),所以时间复杂度为O(n^2)。
class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { sort(nums.begin(),nums.end()); //sort 排序 vector<vector<int>>ans; if(nums.size()<3)return ans; int i=0,l,r; while(i<nums.size()-2) { l=i+1; r=nums.size()-1; while(l<r) { if(nums[l]+nums[r]<-nums[i])l++; else if(nums[l]+nums[r]>-nums[i])r--; else { vector<int>temp; temp.push_back(nums[i]); temp.push_back(nums[l]); temp.push_back(nums[r]); ans.push_back(temp); l++; while(l<r&&nums[ 4000 l]==nums[l-1]) { l++; } } } i++; while(i<nums.size()-2&&nums[i]==nums[i-1]) { i++; } } return ans; } };
【3Sum Closet】
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的值距离最短的那个和。
思路:此题与上面那个题基本类似,甚至更简单一些,只需要比较和的结果即可,碰到和等于target的时候就直接返回吧!!!
class Solution { public: int abs(int x) { return x>0?x:-x; } int threeSumClosest(vector<int>& nums, int target) { int i; int l,r; int min=INT_MAX/2; int len=nums.size(); sort(nums.begin(),nums.end()); for(i=0;i<len-2;i++) { l=i+1; r=len-1; while(l<r) { int sum=nums[l]+nums[r]+nums[i]; if(abs(sum-target)<abs(min-target))min=sum; if(sum==target)return target; else if(sum>target)r--; else l++; } } return min; } };
【4Sum】
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: 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]
]
题意:给一个数组,和一个target整数,求数组中能够使得和为target的所有组合a,b,c,d并满足a<=b<=c<=d。
思路:与上面的3Sum很相似,只不过多了一重循环。
class Solution { public: vector<vector<int>> fourSum(vector<int>& nums, int target) { int len=nums.size(); int i,j,l,r; sort(nums.begin(),nums.end()); vector<vector<int>>ans; set<vector<int>>temp1; for(i=0;i<len-3;i++) { for(j=i+1;j<len-2;j++) { l=j+1; r=len-1; while(l<r) { if(nums[i]+nums[j]+nums[l]+nums[r]==target) { temp1.insert({nums[i],nums[j],nums[l],nums[r]}); l++; //注意 :找到一组之后要l++或者r-- 否则会陷入死循环 } else if(nums[i]+nums[j]+nums[l]+nums[r]>target)r--; else l++; } } } //set去重 auto it=temp1.begin(); for(;it!=temp1.end();it++) ans.push_back(*it); return ans; } };
相关文章推荐
- leetcode 15 3sum & leetcode 18 4sum
- [leetcode 15] 3sum && [leetcode 16] 3sum closest
- leetcode 3sum closest && 4sum
- 【C++】【LeetCode】15. 3Sum & 16. 3Sum Closest & 18. 4Sum
- Leetcode 1. Two Sum & 15. 3Sum & 16. 3Sum Closest & 18. 4Sum
- leetcode 15. 3sum && 16. 3Sum Closest && 18. 4Sum
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
- 【LeetCode】3Sum && 3Sum Closest && 4Sum
- LeetCode 3Sum&&3Sum Closest&&4Sum
- [Leetcode] #1#15#18 2Sum & 3Sum & 4Sum
- Two Sum & 3Sum & 3Sum Closest & 4Sum
- 3Sum & 3Sum Closest & 4Sum
- LeetCode——3Sum & 3Sum Closest
- leetcode 1|167.two sum 1|2 && 3.three sum && 16.3Sum Closest && 18. 4Sum
- 1. Two Sum && 167. Two Sum II - Input array is sorted && 15. 3Sum && 16. 3Sum Closest && 18. 4Sum
- 【小熊刷题】3Sum Closest <Leetcode 16, Java>
- LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number
- 《leetCode》:3Sum Closest
- Sum—LeetCode-15 3Sum
- leetcode ----16. 3Sum Closest