-
Notifications
You must be signed in to change notification settings - Fork 0
/
18.cpp
37 lines (32 loc) · 861 Bytes
/
18.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<bits/stdc++.h>
using namespace std;
class Solution {
private:
vector<vector<int>> result;
unordered_multiset<int> numset;
void nSum(int n, int target, unordered_multiset<int>::iterator pos, unordered_multiset<int> exclude){
if(n == 1){
if(numset.count(target) > exclude.count(target)){
vector<int> match {exclude.begin(), exclude.end()};
match.push_back(target);
result.push_back( match );
}
}
else{
for(auto it = pos; it != numset.end(); it++){
exclude.insert( *it );
auto new_pos = it; new_pos++;
nSum(n - 1, target - *it, new_pos, exclude);
}
}
}
public:
vector<vector<int>> fourSum(vector<int>& nums, int target){
result.clear(); numset.clear();
numset.insert(nums.begin(), nums.end());
nSum(4, target, numset.begin(), {});
return result;
}
};
int main(){
}