classSolution { public: vector<vector<int>> fourSum(vector<int>& nums, int target) { vector<vector<int>> ans; sort(nums.begin(), nums.end()); int n = nums.size(); for (int i = 0; i < n - 3; i++) { if (i > 0 && nums[i] == nums[i-1]) continue; for (int j = i + 1; j < n - 2; j++) { if (j > i + 1 && nums[j] == nums[j - 1]) continue; int lo = j + 1; int hi = n - 1; while (lo < hi) { int sum = nums[i] + nums[j] + nums[lo] + nums[hi]; if (sum == target) { ans.push_back(vector<int>{nums[i], nums[j], nums[lo], nums[hi]}); while (lo < hi && nums[lo] == nums[lo + 1]) { lo++; } while (lo < hi && nums[hi] == nums[hi - 1]) { hi--; } lo++, hi--; } elseif (sum > target) { hi--; } else { lo++; } } } } return ans; } };