-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
57 lines (43 loc) · 1.15 KB
/
main.cpp
File metadata and controls
57 lines (43 loc) · 1.15 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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//Solution class
//Added Test Comment
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> result;
vector<int> copy;
copy = nums;
sort(copy.begin(),copy.end());
for(int i = 0,j = nums.size()-1;;){
if(copy[i] + copy[j] < target)
i++;
else if(copy[i] + copy[j] > target)
j--;
else{
int index1 = find(nums.begin(),nums.end(),copy[i]) - nums.begin();
int index2;
if(copy[i] == copy[j]){
index2 = find(nums.begin()+index1 + 1 ,nums.end(),copy[j]) - nums.begin();
// index2 = find(nums.begin(),nums.end(),copy[j]) - nums.begin();
}
else
index2 = find(nums.begin(),nums.end(),copy[j]) - nums.begin();
result.push_back(index1);
result.push_back(index2);
return result;
}
}
}
};
int main()
{
vector<int> v = {3,2,3};
int target = 6;
Solution sol;
vector<int> res = sol.twoSum(v, target);
for(int i=0; i< res.size(); i++)
cout<<res[i]<<" "<<endl;
}