15. 3Sum(中等)

时间:2021-05-23 18:35:02

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]
]

人家想法:

0. 先排序,再次注意C++排序的使用:sort(A.begin(), A.end());

  1. 固定1个值(用i指向),另两个值依据 sum=0 - A[i] 的情况,通过两个指针lo, hi 往中间扫;

    具体的:
  2. A[lo] + A[hi] == sum时,lo++, hi--;
  3. A[lo] + A[hi] < sum时,说明和太小,那就向右移动 lo 指针;
  4. A[lo] + A[hi] > sum时,说明和太大,那就向左移动 hi 指针;
  5. 消除i, lo, hi指针处的重复值 是另一个难点,注意观察下面程序咋做的.

自个代码及注释:

\(O(n^2)\) time, \(O(1)\) space.

vector<vector<int>> threeSum(vector<int>& A) {
int n = A.size();
sort(A.begin(), A.end());
vector<vector<int>> res;
for (int i = 0; i < n - 2; i++) {
if (A[i] > 0) break;
// 消除i处重复值
if (i == 0 || (i > 0 && A[i] != A[i - 1])) {
int lo = i + 1, hi = n - 1, sum = 0 - A[i];
while (lo < hi) {
if (A[lo] + A[hi] == sum) {
// 精华之处
// 若相等,则移动lo, hi,不可只移动lo或hi,因为这是增序
vector<int> triplet(3, 0);
triplet[0] = A[i];
triplet[1] = A[lo];
triplet[2] = A[hi];
res.push_back(triplet); //消除lo,hi处重复值
while (lo < hi && (A[lo] == A[lo + 1])) lo++;
while (lo < hi && (A[hi] == A[hi - 1])) hi--;
lo++; hi--;
}
//此处无需消除重复值,大不了lo,hi之和仍小于sum,继续移动就是了
else if (A[lo] + A[hi] < sum) lo++;
else hi--;
}
}
}
return res;
}