-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoSum.cpp
More file actions
31 lines (27 loc) · 804 Bytes
/
Copy pathtwoSum.cpp
File metadata and controls
31 lines (27 loc) · 804 Bytes
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
#include<iostream>
#include<vector>
#include<algorithm>
class Solution {
public:
std::vector<int> twoSum(std::vector<int>& nums, int target) {
for(int i = 0; i < nums.size(); i++){
std::vector<int>::iterator it = std::find(nums.begin(), nums.end(), target-nums[i]);
if(it!=nums.end() && std::distance(nums.begin(), it)!=i){
std::vector<int> ans;
ans.push_back(i);
ans.push_back(std::distance(nums.begin(), it));
return ans;
}
}
return {-1,-1};
}
};
int main() {
Solution solution;
std::vector vector = {2,7,11,5};
for(auto n : solution.twoSum(vector,9)) {
std::cout << n << " ";
}
std::cout << std::endl;
return 0;
}