-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo_sum.cpp
39 lines (38 loc) · 1.12 KB
/
two_sum.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
// O(N) solution
// Unordered map to allow lookup of previous elements with O(1)
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> v;
unordered_map<int, int> seenSet;
for (int i=0; i<nums.size(); i++) {
if (seenSet.find(target-nums[i]) == seenSet.end()) {
// Not possible for current element to add up with previous element
seenSet[nums[i]] = i;
} else {
// Current + previous = target; return
v = {seenSet[target-nums[i]], i};
break;
}
}
return v;
}
};
// O(N^2) solution
// Nested loop checks all permutations for condition match
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> v;
// Nested loop match condition
for (int i=0; i<nums.size(); i++) {
for (int j=i+1; j<nums.size(); j++) {
if (nums[i] + nums[j] == target) {
v = {i,j};
break;
}
}
}
return v;
}
};