class Solution {
public:
    vector<vector<int> > threeSum(vector<int> &num) {
        sort(num.begin(), num.end());
        vector<vector<int> > res;
        if (num.size() < 3) {
            return res;
        }
        for (int i = 0; i < num.size() - 2; i++) {
            int left = i + 1;
            int right = num.size() - 1;
            
            while (left < right) {
                int sum = num[i] + num[left] + num[right];
                if (sum > 0) {
                    right--;
                } else if(sum < 0) {
                    left++;
                } else {
                    vector<int> current = {num[i], num[left], num[right]};
                    res.push_back(current);
                    while (left + 1 < right && num[left+1] == num[left]) {
                        left++;
                    } // 防止重复
                    while(left + 1 < right && num[right-1] == num[right]) {
                        right--;
                    } // 防止重复
                    ++left; --right;
                }
            }
            while (i + 1 < num.size() - 2 && num[i+1] == num[i]) {
                i++;
            }   // 防止重复
        }
        return res;
    }
};