Untitled

 avatar
user_9124840
plain_text
12 days ago
853 B
3
Indexable
Never
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        cin.tie(nullptr)->sync_with_stdio(0);
        int sz = nums.size();
        sort(nums.begin(), nums.end());
        vector<vector<int>> ans;
        set<vector<int>> filter;

        for (int i = 0; i < sz; i++) {

            int l = i + 1, r = sz - 1;

            while (l < r) {
                if (nums[l] + nums[r] + nums[i] > 0) {
                    r -= 1;
                } else if (nums[l] + nums[r] + nums[i] < 0) {
                    l += 1;
                } else {
                    filter.insert({nums[i], nums[l], nums[r]});
                    l += 1;
                    r -= 1;
                }
            }
        }

        for (auto it : filter)
            ans.push_back(it);
        return ans;
    }
};
Leave a Comment