Last active
December 31, 2015 07:58
-
-
Save soulmachine/7957173 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LeetCode, 4Sum | |
// 用一个 hashmap 先缓存两个数的和 | |
// 时间复杂度O(n^2),空间复杂度O(n^2) | |
// @author 龚陆安(http://weibo.com/luangong) | |
class Solution { | |
public: | |
vector<vector<int>> fourSum(vector<int>& num, int target) { | |
vector<vector<int>> result; | |
if (num.size() < 4) return result; | |
sort(num.begin(), num.end()); | |
unordered_multimap<int, pair<int, int>> cache; | |
for (int i = 0; i + 1 < num.size(); ++i) | |
for (int j = i + 1; j < num.size(); ++j) | |
cache.insert(make_pair(num[i] + num[j], make_pair(i, j))); | |
for (auto i = cache.begin(); i != cache.end(); ++i) { | |
int x = target - i->first; | |
auto range = cache.equal_range(x); | |
for (auto j = range.first; j != range.second; ++j) { | |
auto a = i->second.first; | |
auto b = i->second.second; | |
auto c = j->second.first; | |
auto d = j->second.second; | |
if (a != c && a != d && b != c && b != d) { | |
vector<int> vec = { num[a], num[b], num[c], num[d] }; | |
sort(vec.begin(), vec.end()); | |
result.push_back(vec); | |
} | |
} | |
} | |
sort(result.begin(), result.end()); | |
result.erase(unique(result.begin(), result.end()), result.end()); | |
return result; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment