Given an array nums of n integers, are there elements a, b, c in nums 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.
Example:
Given array nums = [-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) {
        vector<vector<int>> ans;
        if (nums.size() == 0) {
            return ans;
        }
        sort(nums.begin(), nums.end());
        for (int i = 0; i < nums.size() && nums[i] <= 0; ++i) {
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            int sum = -nums[i];
            for (int j = i + 1, k = nums.size() - 1; j < k;) {
                int tmp = nums[j] + nums[k];
                if (sum == tmp) {
                    ans.push_back(vector<int>{nums[i], nums[j], nums[k]});
                    while (j < nums.size() - 1 && nums[j] == nums[j + 1]) {
                        ++j;
                    }
                    while (k > 0 && nums[k] == nums[k - 1]) {
                        --k;
                    }
                    ++j;
                    --k;
                }
                else if (tmp < sum) {
                    ++j;
                }
                else {
                    --k;
                }
            }
        }
        return ans;
    }
};原文:https://www.cnblogs.com/yhjd/p/10652006.html