018. 4Sum

方法一:超时,最坏情况下O(N^4),平均O(N^2)

 1 class Solution {
 2 public:
 3     vector<vector<int>> fourSum(vector<int>& nums, int target) {
 4         vector<vector<int>> result;
 5         if (nums.size() < 4) return result;
 6         map<int, vector<pair<int, int>>> cache;
 7         set<vector<int>> res;
 8         sort(nums.begin(), nums.end());
 9         for (int i = 0; i < nums.size() - 1; ++i) {
10             for (int j = i + 1; j < nums.size(); ++j) {
11                 cache[nums[i] + nums[j]].push_back(pair<int, int>(i, j));
12             }
13         }
14         for (int i = 0; i < nums.size() - 3; ++i) {
15             for (int j = i + 1; j < nums.size() - 2; ++j) {
16                 int gap = target - nums[i] - nums[j];
17                 if (cache.find(gap) != cache.end()) {
18                     vector<int> temp;
19                     for (auto item : cache[gap]) {
20                         temp.clear();
21                         if (i != item.first && i != item.second && j != item.first && j != item.second) {
22                             temp = vector<int>{ nums[i], nums[j], nums[item.first], nums[item.second] };
23                             sort(temp.begin(), temp.end());
24                             res.insert(temp);
25                         }
26                     }
27                 }
28             }
29         }
30         result = vector<vector<int>>(res.begin(), res.end());
31         return result;
32     }
33 };

方法二:O(N^2),不知道问题出在哪里了,仍然超时。。

 1 class Solution {
 2 public:
 3     vector<vector<int>> fourSum(vector<int>& nums, int target) {
 4         vector<vector<int>> result;
 5         if (nums.size() < 4) return result;
 6         else {
 7             multimap<int, pair<int, int>> cache;
 8             for (int i = 0; i < nums.size() - 1; ++i) {
 9                 for (int j = i + 1; j < nums.size(); ++j) {
10                     cache.insert(pair<int, pair<int, int>>(nums[i] + nums[j], pair<int, int>(i, j)));
11                 }
12             }
13             for (const auto& item : cache) {
14                 int gap = target - item.first;
15                 auto range = cache.equal_range(gap);
16                 for (auto i = range.first; i != range.second; ++i) {
17                     int a = item.second.first;
18                     int b = item.second.second;
19                     int c = i->second.first;
20                     int d = i->second.second;
21                     if (a != c && a != d && b != c && b != d) {
22                         vector<int> vec{ nums[a], nums[b], nums[c], nums[d] };
23                         sort(vec.begin(), vec.end());
24                         result.push_back(vec);
25                     }
26                 }
27             }
28             sort(result.begin(), result.end());
29             result.erase(unique(result.begin(), result.end()), result.end());
30             return result;
31         }
32     }
33 };
时间: 2024-11-09 22:45:45

018. 4Sum的相关文章

[LeetCode] 018. 4Sum (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 018.4Sum (Medium) 链接: 题目:https://oj.leetcode.com/problems/4sum/ 代码(github):https://github.com/illuz/leetcode 题意: 给一个数列 S ,找出四个数 a,b,c,d 使得a + b + c + d = targ

【LeetCode】018 4Sum

题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: The solution set must not contain duplicate quadruplets. For ex

LeetCode 018 4Sum

题目描述:4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descend

Java for LeetCode 018 4Sum

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. 解题思路: 首先肯定是一次排序,如果是暴力枚举的话,肯定超时.因此,我们可以采用分治的思想,存储所有2个元素的和,然后采用2SUM的思路求解即可,

[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

全部最新的题解可以在 我的 github 上找,欢迎 star 和 watch ~ 更新中~~ 说明 这个系列的题解包括用 C++/Java/Python 写的 leetcode 上的算法题目,和 Sql 写的 leetcode 上的数据库题目. 有些题目虽然 AC 了却还没写分析,所以这次就开坑来完成. 链接: 我的 github Leetcode Algorithms Problems Leetcode Database Problems CSDN 题解索引 001.Two_Sum (Med

No.018:4Sum

题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: The solution set must not contain duplicate quadruplets. For ex

[Leetcode] 4Sum

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending order.

LeetCode 017 4Sum

[题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending o

LeetCode: Letter Combinations of a Phone Number [018]

[题目] Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae"