class Solution {public: vector > threeSum(vector & nums) { set > res; sort(nums.begin(), nums.end()); if (nums.empty() || nums.back() < 0 || nums.front() > 0) return {}; for (int k = 0; k < nums.size(); ++k) { if (nums[k] > 0) break; int target = 0 - nums[k]; int i = k + 1, j = nums.size() - 1; while (i < j) { if (nums[i] + nums[j] == target) { res.insert({nums[k], nums[i], nums[j]}); while (i < j && nums[i] == nums[i + 1]) ++i; while (i < j && nums[j] == nums[j - 1]) --j; ++i; --j; } else if (nums[i] + nums[j] < target) ++i; else --j; } } return vector >(res.begin(), res.end()); }};