-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1.cpp
More file actions
28 lines (28 loc) · 1.01 KB
/
Copy path1.cpp
File metadata and controls
28 lines (28 loc) · 1.01 KB
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
// Author: btjanaka (Bryon Tjanaka)
// Problem: (Leetcode) 1
// Title: Two Sum
// Link: https://leetcode.com/problems/two-sum/
// Idea: The easy solution is to check every pair of elements, but this is
// O(n^2). To achieve O(n), the idea is to use a hash set that keeps track of
// which elements we have seen so far while iterating through the list. Then,
// for the value x, we can check if we have target - x in the set. In this
// solution, we specifically use a hash map instead of a set because we need to
// store the indices.
// Difficulty: easy
// Tags: set
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> numtoindex;
for (int i = 0; i < nums.size(); ++i) {
int curr = nums[i];
if (numtoindex.find(curr) != numtoindex.end()) {
return {numtoindex[curr], i};
}
numtoindex[target - curr] = i;
}
// Technically not necessary, as the problem specifies we will always have
// exactly one solution.
return {-1, -1};
}
};