forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_1.cpp
More file actions
24 lines (23 loc) · 668 Bytes
/
_1.cpp
File metadata and controls
24 lines (23 loc) · 668 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
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> m;
vector<int> result;
for(int i = 0; i < nums.size(); i++){
// not found the second one
if (m.find(nums[i]) == m.end() ) {
// store the first one poisition into the second one's key
m[target - nums[i]] = i;
} else {
// found the second one
result.push_back(m[nums[i]]);
result.push_back(i);
break;
}
}
return result;
}
};