-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwo_sum.cpp
More file actions
30 lines (25 loc) · 989 Bytes
/
Two_sum.cpp
File metadata and controls
30 lines (25 loc) · 989 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
#include <vector>
#include <unordered_map>
class Solution {
public:
std::vector<int> twoSum(std::vector<int>& nums, int target) {
// Create an unordered map to store the indices of each number
std::unordered_map<int, int> seen;
// Loop through each number in the input vector
for (int i = 0; i < nums.size();++i) {
int num = nums[i];
// Calculate the complement that will add up to the target
int complement = target - num;
// Check if the complement exists in the map
if (seen.find(complement) != seen.end()) {
// Return the indices as a vector
return {seen[complement], i};
}
// Add the current number and its index to the map
seen[num] = i;
}
// Return an empty vector if no solution is found
// (Although the problem guarantees that there is exactly one solution)
return {};
}
};