# 三数之和

给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。

注意:答案中不可以包含重复的三元组。

 

示例 1:

输入:nums = [-1,0,1,2,-1,-4]
输出:
[[-1,-1,2],[-1,0,1]]

示例 2:

输入:nums = []
输出:
[]

示例 3:

输入:nums = [0]
输出:
[]

 

提示:

以下错误的选项是?

## aop ### before ```cpp #include #include #include using namespace std; ``` ### after ```cpp int main() { Solution sol; int arr[] = {-1,0,1,2,-1,-4}; int length1 = sizeof(arr) / sizeof(arr[0]); vector nums(arr, arr + length1); vector > res; res = sol.threeSum(nums); for (auto i : res) { for (auto j : i) cout << j << ' '; cout << endl; } return 0; } ``` ## 答案 ```cpp class Solution { public: vector> threeSum(vector &nums) { sort(nums.begin(), nums.end()); int len = nums.size(); vector> ans; if (nums.empty()) return {}; for (int i = 0; i < len; ++i) { if (i > 0 && nums[i] == nums[i - 1]) continue; int newtar = -nums[i]; int low = i + 1; int high = len - 1; while (low < high) { if (nums[low] + nums[high] == newtar) { ans.push_back({nums[i], nums[low], nums[high]}); while (low < high && nums[low] == nums[low + 1]) ++low; while (low < high && nums[high] == nums[high - 1]) --high; } else high--; } } return ans; } }; ``` ## 选项 ### A ```cpp class Solution { public: vector> threeSum(vector &nums) { sort(nums.begin(), nums.end()); if (nums.size() < 3 || nums.front() > 0 || nums.back() < 0) return {}; vector> res; for (int i = 0; i < nums.size(); i++) { int fix = nums[i]; if (fix > 0) break; if (i > 0 && fix == nums[i - 1]) continue; int l = i + 1; int r = nums.size() - 1; while (l < r) { if (nums[l] + nums[r] == -fix) { if (l == i + 1 || r == nums.size() - 1) { res.push_back(vector{nums[i], nums[l], nums[r]}); l++; r--; } else if (nums[l] == nums[l - 1]) l++; else if (nums[r] == nums[r + 1]) r--; else { res.push_back(vector{nums[i], nums[l], nums[r]}); l++; r--; } } else if (nums[l] + nums[r] < -fix) l++; else r--; } } return res; } }; ``` ### B ```cpp class Solution { public: static bool cmp(const int &a, const int &b) { return a < b; } vector> threeSum(vector &nums) { sort(nums.begin(), nums.end(), cmp); vector::iterator it; vector> res; set> a; for (int i = 0; i < nums.size(); i++) { if (nums[i] > 0) break; for (int j = i + 1; j < nums.size(); j++) { it = find(nums.begin() + j + 1, nums.end(), 0 - nums[i] - nums[j]); if (it != nums.end()) { vector temp; temp.push_back(nums[i]); temp.push_back(nums[j]); temp.push_back(*it); a.insert(temp); } } } for (auto k : a) { res.push_back(k); } return res; } }; ``` ### C ```cpp class Solution { public: vector > threeSum(vector &nums) { if (nums.size() < 3) return {}; vector > res; set > ret; for (int i = 0; i < nums.size() - 2; i++) { for (int j = i + 1; j < nums.size() - 1; j++) { for (int k = j + 1; k < nums.size(); k++) { if (nums[i] + nums[k] + nums[j] == 0) { vector temp; int a = (nums[i] < nums[j] ? nums[i] : nums[j]) < nums[k] ? (nums[i] < nums[j] ? nums[i] : nums[j]) : nums[k]; int b = (nums[i] > nums[j] ? nums[i] : nums[j]) > nums[k] ? (nums[i] > nums[j] ? nums[i] : nums[j]) : nums[k]; int c = 0 - a - b; temp.push_back(a); temp.push_back(c); temp.push_back(b); ret.insert(temp); } } } } for (auto it : ret) { res.push_back(it); } return res; } }; ```