您的位置:首页 > 其它

LeetCode Two Sum、3Sum、3Sum Closest、4Sum

2018-03-23 16:30 411 查看
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same element twice.Example:
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int,int>mp;
vector<int>res;
int len = nums.size();
for(int i=0;i<len;i++)
mp[nums[i]]=i;
for(int i=0;i<len;i++)
{
if(mp[target-nums[i]])
{
res.push_back(i);
res.push_back(mp[target-nums[i]]);
return res;
}
}
}
};
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]
]
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int len=nums.size();
vector<vector<int> > res;
if(len<=2) return res;
sort(nums.begin(),nums.end());
for(int i=0;i<len;)
{
int st=i+1,ed=len-1;
while(st<ed)
{
if(nums[i]+nums[st]+nums[ed]==0)
{
res.push_back({nums[i],nums[st],nums[ed]});
st++;
ed--;
while(st<ed&&nums[st]==nums[st-1]) st++;
while(st<ed&&nums[ed]==nums[ed+1]) ed--;
}
else if(nums[i]+nums[st]+nums[ed]<0)
{
st++;
while(st<ed&&nums[st]==nums[st-1]) st++;
}
else if(nums[i]+nums[st]+nums[ed]>0)
{
ed--;
while(st<ed&&nums[ed]==nums[ed+1]) ed--;
}
}
i++;
while((i<len)&&(nums[i]==nums[i-1]))
i++;
}
return res;
}
};
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).
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int len=nums.size();
sort(nums.begin(),nums.end());
int res=nums[0]+nums[1]+nums[2];
if(len==3) return res;
for(int i=0;i<len-2;i++)
{
int st=i+1,ed=len-1;
while(st<ed)
{
int sum=nums[i]+nums[st]+nums[ed];
if(abs(sum-target)<abs(res-target))
{
res=sum;
if(res==target)
return res;
}
if(sum>target)
ed--;
else
st++;
}
}
return res;
}
};
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]
]
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> res;
int len=nums.size();
if(len<4) return res;
sort(nums.begin(),nums.end());
for(int i=0;i<len-3;i++)
{
if(i>0&&nums[i]==nums[i-1]) continue;
if(nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target) break;
if(nums[i]+nums[len-1]+nums[len-2]+nums[len-3]<target) continue;
for(int j=i+1;j<len-2;j++)
{
if(j>i+1&&nums[j]==nums[j-1]) continue;
if(nums[i]+nums[j]+nums[j+1]+nums[j+2]>target) break;
if(nums[i]+nums[j]+nums[len-1]+nums[len-2]<target) continue;
int st=j+1,ed=len-1;
while(st<ed)
{
int sum=nums[i]+nums[j]+nums[st]+nums[ed];
if(sum==target)
{
res.push_back({nums[i],nums[j],nums[st],nums[ed]});
ed--;
while(st<ed&&nums[ed]==nums[ed+1])
ed--;
st++;
while(st<ed&&nums[st]==nums[st-1])
st++;
}
else if(sum>target)
{
ed--;
while(st<ed&&nums[ed]==nums[ed+1])
ed--;
}
else if(sum<target)
{
st++;
while(st<ed&&nums[st]==nums[st-1])
st++;
}
}
}
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: