-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1.2sum-repeat.cpp
51 lines (47 loc) · 973 Bytes
/
1.2sum-repeat.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* 不允许重复的多个2sum问题
*/
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
vector<int> twoSum(vector<int> &nums, int target)
{
sort(nums.begin(), nums.end());
vector<int> res;
int lo = 0, hi = nums.size() - 1;
while (lo < hi)
{
int left = nums[lo], right = nums[hi];
int sum = left + right;
if (sum < target)
lo++;
else if (sum < target)
hi--;
else
{
res.push_back(left);
res.push_back(right);
while (lo < hi && nums[lo] == left)
lo++;
while (lo < hi && nums[hi] == right)
hi--;
}
}
return res;
}
};
int main()
{
Solution s;
vector<int> nums = {1, 3, 1, 2, 2, 3};
vector<int> sol = s.twoSum(nums, 4);
/* Print path vector to console */
for (auto i = sol.begin(); i != sol.end(); ++i)
std::cout << *i << ' ';
cout << endl;
return 0;
}