-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.cpp
More file actions
executable file
·38 lines (32 loc) · 857 Bytes
/
Copy path1.cpp
File metadata and controls
executable file
·38 lines (32 loc) · 857 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
31
32
33
34
35
36
37
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
for (int i = 0; i < nums.size(); i++) {
for (int j = 0; j < nums.size(); j++) {
if (i != j && i < j) {
if (nums[i] + nums[j] == target) {
return {i,j};
}
}
}
}
return {};
}
};
// Optimized Solution
// TC - O(n)
// SC - O(n)
/* vector<int> twoSum(vector<int>& nums, int target){
int n = nums.size();
unordered_map<int, int> prevMap;
for (int i=0; i<n; i++){
int diff = target - nums[i];
if (prevMap.find(diff) != prevMap.end())
return {prevMap[diff], i};
prevMap.insert({nums[i], i});
}
return {};
} */