-
Notifications
You must be signed in to change notification settings - Fork 0
/
leet_1.cpp
39 lines (35 loc) · 910 Bytes
/
leet_1.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
#include <vector>
#include <iostream>
#include <unordered_map>
using namespace std;
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, vector<int> > valueMap;
// for(auto &x: nums) cout << x << "\n";
vector<int> answer;
for(int i=0; i<nums.size(); ++i) valueMap[nums[i]].push_back(i);
for(int i=0; i<nums.size(); ++i){
auto it = valueMap.find(target - nums[i]);
if(it != valueMap.end()){
for(auto &x: it->second){
if(x != i){
if(i < x){
answer.push_back(i+1);
answer.push_back(x+1);
} else {
answer.push_back(x+1);
answer.push_back(i+1);
}
break;
}
}
}
if(!answer.empty()) break;
}
return answer;
}
int main(){
vector<int> nums = {2, 7, 11, 15};
vector<int> answer = twoSum(nums, 17);
cout << answer[0] << " " << answer[1] << "\n";
return 0;
}