题解 | #三数之和#
三数之和
https://www.nowcoder.com/practice/345e2ed5f81d4017bbb8cc6055b0b711
class Solution { public: vector<vector<int> > threeSum(vector<int> &nums) { multimap<int, int>m; vector<vector<int>>ans; for(int i = 0; i < nums.size(); i ++){ for(int j = i + 1; j < nums.size(); j ++){ if(j != i + 1 && nums[j] == nums[j - 1]) continue; m.insert(make_pair(i, j)); } } for(auto i : m){ int sum = nums[i.first] + nums[i.second]; auto it = find(nums.begin(), nums.end(), -sum); if(it != nums.end() && (it - nums.begin()) != i.first && (it - nums.begin()) != i.second){ vector<int>temp = {nums[i.first], nums[i.second], -sum}; sort(temp.begin(), temp.end()); if(find(ans.begin(), ans.end(), temp)==ans.end()) ans.push_back(temp); } } return ans; } };