-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo-sum.cpp
More file actions
57 lines (54 loc) · 1.44 KB
/
two-sum.cpp
File metadata and controls
57 lines (54 loc) · 1.44 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// https://leetcode.com/problems/two-sum/solution/
#include<iostream>
#include <vector>
#include <unordered_map>
using namespace std;
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
int result_i = 0, result_j = 0;
for(int i=0; i< n; i++){
for(int j=i+1; j < n; j++){
if(nums[i] + nums[j] == target){
result_i = i;
result_j = j;
break;
}
}
}
return {result_i, result_j};
}
vector<int> twoSumTwoPassHash(vector<int> &nums, int target){
unordered_map<int, int> ump;
int complement;
int complement_index;
int number_index;
for(int i=0; i< nums.size(); i++){
ump.emplace(nums[i], i);
}
unordered_map<int, int>::iterator finder;
for(int i=0; i< nums.size(); i++){
complement = target - nums[i];
finder = ump.find(complement);
if(finder != ump.end() && ump[complement] != i){
number_index = i;
complement_index = finder -> second;
break;
}
}
if (number_index <= complement_index){
return {number_index, complement_index};
}else{
return {complement_index, number_index};
}
}
int main(){
vector<int> test = {3,3};
vector<int> twoSumResult = twoSum(test, 6);
cout << twoSumResult[0];
cout << twoSumResult[1];
cout << "RESULT USING 2 PASS HASH:" << endl;
vector<int> twoSumTwoPassHashResult = twoSumTwoPassHash(test, 6);
cout << twoSumTwoPassHashResult[0];
cout << twoSumTwoPassHashResult[1];
return 0;
}