-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.cpp
More file actions
21 lines (19 loc) · 730 Bytes
/
Copy path1.cpp
File metadata and controls
21 lines (19 loc) · 730 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// what I wrote is pretty much identical to leetcode's official C++ solution
// except I used more readable variable names.
#include <vector>
#include <unordered_map>
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> indices;
for (int index = 0; index < nums.size(); index++) {
int complement = target - nums[index];
auto complement_index_it = indices.find(complement);
if (complement_index_it != indices.end()) {
return {complement_index_it->second, index};
}
indices[nums[index]] = index;
}
return {};
}
};