一.题目
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)
Show Tags
Have you met this question in a real interview?
Yes
No
二.解题技巧
这道题可以采用和3Sum一样的思路,相同的方法,不过时间复杂度为O(n^3),空间复杂度为O(1)。
这道题也可以在排序之后先计算后面两个数的和,将其方法一个哈希表中,由于可能存在不同的两个数的和为相同值,因此,可以考虑将和为相同的值放在一个链表中,然后将变量头放在哈希表中。然后再按照3Sum的思路,不过第三个数在这里变成了第三个和第四个数的和,通过哈希表可以方便地找到和为固定值的数的链表,就可以找到符合条件的四个数。这种方法的时间复杂度为O(n^2),空间复杂度也为O(n^2)。(第二种方法我还没实现)
三.实现代码
class Solution
{
public:
vector<vector<int> > fourSum(vector<int> &num, int target)
{
vector<vector<int> > Result;
int Size = num.size();
if (Size < 4)
{
return Result;
}
// sort the array
sort(num.begin(), num.end());
for (int Index_first = 0; Index_first < (Size - 3); Index_first++)
{
int First = num[Index_first];
if ((Index_first != 0) && (num[Index_first - 1] == num[Index_first]))
{
continue;
}
for (int Index_second = Index_first + 1; Index_second < (Size - 2); Index_second++)
{
if ((Index_second != (Index_first + 1)) && (num[Index_second - 1] == num[Index_second]))
{
continue;
}
int Second = num[Index_second];
int Index_third = Index_second + 1;
int Index_foud = Size - 1;
while (Index_third < Index_foud)
{
int Third = num[Index_third];
int Fourd = num[Index_foud];
int Sum = First + Second + Third + Fourd;
if (Sum == target)
{
vector<int> Tmp;
Tmp.push_back(First);
Tmp.push_back(Second);
Tmp.push_back(Third);
Tmp.push_back(Fourd);
Result.push_back(Tmp);
Index_third++;
while ((Index_third <= (Size - 1)) && (num[Index_third] == num[Index_third - 1]))
{
Index_third++;
}
Index_foud--;
while ((Index_foud > Index_second) && (num[Index_foud] == num[Index_foud + 1]))
{
Index_foud--;
}
}
if (Sum < target)
{
Index_third++;
while ((Index_third < Size) && (num[Index_third] == num[Index_third - 1]))
{
Index_third++;
}
}
if (Sum > target)
{
Index_foud--;
while ((Index_foud > Index_second) && (num[Index_foud] == num[Index_foud + 1]))
{
Index_foud--;
}
}
}
}
}
return Result;
}
};
四.体会
这道题是3Sum的一种延伸,不过如果仅仅按照3Sum的延伸来做这道题的话,算法的空间复杂度会达到O(n^3),但是空间复杂度为O(1)。可以换一种思路,在排序之后,先计算后面两个数的和,并用哈希表存储起来,然后就将问题转变为了3Ssum的问题,只不过计算出第三个数之后,还需要在哈希表中找到和满足条件的第三个数和第四个数。转化一种思路就可以大大地降低计算复杂度,这个就是思路算法的乐趣所在。
版权所有,欢迎转载,转载请注明出处,谢谢