您的位置:首页 > 其它

Two Sum

2014-08-26 16:37 218 查看
初阶。Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where

index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not

zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9

Output: index1=1, index2=2

思路:

此题需要返回的是下标,不是数字本身。

思路1。暴力,时间复杂度o(n^2)

思路2。用一个hash表,存储每个数对应的下标,复杂度O(n).

class Solution{
public:
vector<int> twoSum(vector<int> &num,int target)
{
unordered_map<int,int> mapping;
vector<int> result;

for(unsigned int i=0;i<num.size();i++)
{
mapping[num[i]] = i;
}

for(unsigned int i=0;i<num.size();i++)
{
const int gap = target - num[i];
if (mapping.find(gap)!=mapping.end() && mapping[gap] > i)
{
result.push_back(i+1);
result.push_back(mapping[gap]+1);
break;
}
}
return result;
}
};


进阶。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:

?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)

思路:先排序,然后左右夹逼。复杂度O(n^2)

class Solution{
public:
vector<vector<int>> threeNum(vector<int> &num)
{
vector<vector<int>> result;
if (num.size() < 3) return result;
sort(num.begin(),num.end());
int target = 0;
vector<int> rec;

auto last = num.end();
for(auto a = num.begin();a < prev(last,2);a++)
{
auto b = next(a);
auto c = prev(last);
while(b<c)
{
if(*a + *b + *c < target)
{
++b;
}else if(*a + *b + *c > target)
{
--c;
}
else{
//result.push_back({*a, *b, *c});
rec.clear();
rec.push_back(*a);
rec.push_back(*b);
rec.push_back(*c);
result.push_back(rec);
++b;
--c;
}
}
}
sort(result.begin(),result.end());
result.erase(unique(result.begin(),result.end()),result.end());
return result;
}
};


进阶+.3Sum Closest

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).

思路。同上,先排序,然后再左右夹逼。复杂度O(n^2).

进阶++。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:

?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)

思路。

思路1,先排序,然后左右夹逼。复杂度O(n^3).

思路2,用一个hashmap先缓存两个数的和,时间复杂度,平均O(n^2),最坏o(n^4),空间复杂度O(n^2).

思路3,用multimap.时间复杂度O(n^2),空间复杂度O(n^2).
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: