-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4SUM2.cpp
More file actions
58 lines (41 loc) · 1.22 KB
/
Copy path4SUM2.cpp
File metadata and controls
58 lines (41 loc) · 1.22 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//https://leetcode.com/problems/4sum/
#include <bits/stdc++.h>
#include <iostream>
#include <sstream>
#include <string>
#define pair<int, int> pp
using namespace std;
bool compare(pp const &a, pp const &b) {
return a.second <= b.second;
}
vector<int> nums={1,0,-1,0,-2,2};
int target = 0;
int main() {
vector<vector<int>> ans;
vector<vector<int>> index;
vector<pair<int, int>> mm;
for(int i=0;i<nums.size();i++){
mm.emplace_back(make_pair(i, nums[i]));
}
sort(mm.begin(), mm.end(), compare);
sort(nums.begin(), nums.end());
int sz = nums.size();
int i,j, k;
for(int a=0;a<sz-3;a++){
for(int b=a+1;b<sz-2;b++){
int left, right, val;
val = nums[a]+nums[b]+nums[left]+nums[right];
while(nums[left]<nums[right]){
if(val == target){
ans.emplace_back({nums[a], nums[b], nums[left], nums[right]});
index.emplace_back({a, b, left, right});
}else if(val < target){
left++;
}else{
right++;
}
}
}
}
return 0;
}