# 四数之和 以下错误的选项是? ## aop ### before ```cpp #include using namespace std; ``` ### after ```cpp int main() { Solution sol; int target = 0; int nums[] = {1, 0, -1, 0, -2, 2}; int length1 = sizeof(nums) / sizeof(nums[0]); vector nums1(nums, nums + length1); vector> res; res = sol.fourSum(nums1, target); for (auto i : res) { for (auto j : i) cout << j << ' '; cout << endl; } return 0; } ``` ## 答案 ```cpp class Solution { public: vector> fourSum(vector &nums, int target) { vector> res; vector temp; sort(nums.begin(), nums.end()); for (int base_begin = 0; base_begin < nums.size(); base_begin++) { if (base_begin > 0 && nums.at(base_begin) == nums.at(base_begin - 1)) continue; for (int base_end = nums.size() - 1; base_end > base_begin + 2; base_end--) { int left = base_begin + 1; int right = base_end - 1; while (left < right) { int tt = nums.at(base_begin) + nums.at(base_end) + nums.at(left) + nums.at(right); if (tt > target) right--; else { temp.clear(); temp.push_back(nums.at(base_begin)); temp.push_back(nums.at(left)); temp.push_back(nums.at(right)); temp.push_back(nums.at(base_end)); res.push_back(temp); ++left; --right; } } } } return res; } }; ``` ## 选项 ### A ```cpp class Solution { public: vector> fourSum(vector &nums, int target) { vector> res; sort(nums.begin(), nums.end()); if (nums.size() < 4) return res; for (int a = 0; a < nums.size() - 3; ++a) { if (a > 0 && nums[a] == nums[a - 1]) { continue; } for (int b = a + 1; b < nums.size() - 2; ++b) { if (b > a + 1 && nums[b] == nums[b - 1]) { continue; } int c = b + 1, d = nums.size() - 1; while (c < d) { int sum = nums[a] + nums[b] + nums[c] + nums[d]; if (sum < target) { c++; } else if (sum > target) { d--; } else { res.push_back({nums[a], nums[b], nums[c], nums[d]}); while (c < d && nums[c] == nums[c + 1]) { c++; } while (c < d && nums[d - 1] == nums[d]) { d--; } c++; d--; } } } } return res; } }; ``` ### B ```cpp class Solution { public: vector> fourSum(vector &nums, int target) { if (nums.size() < 4) return {}; sort(nums.begin(), nums.end()); vector> res; set> a; for (int i = 0; i < nums.size() - 3; i++) { if (nums[i] > target && target > 0) break; for (int j = i + 1; j < nums.size() - 2; j++) { int l = j + 1; int r = nums.size() - 1; while (l < r) { if (nums[i] + nums[j] + nums[l] + nums[r] < target) l++; else if (nums[i] + nums[j] + nums[l] + nums[r] > target) r--; else { vector temp{nums[i], nums[j], nums[l], nums[r]}; a.insert(temp); l++; r--; } } } } for (auto c : a) { res.push_back(c); } return res; } }; ``` ### C ```cpp class Solution { public: vector> fourSum(vector &nums, int target) { if (nums.size() < 4) return {}; sort(nums.begin(), nums.end()); set> a; vector> res; for (int i = 0; i < nums.size() - 3; i++) { if (nums[i] > target && target > 0) break; for (int j = i + 1; j < nums.size() - 2; j++) { for (int l = j + 1; l < nums.size() - 1; l++) { for (int r = l + 1; r < nums.size(); r++) { if (nums[i] + nums[j] + nums[l] + nums[r] == target) a.insert(vector{nums[i], nums[j], nums[l], nums[r]}); } } } } for (auto c : a) { res.push_back(c); } return res; } }; ```