Last active
August 18, 2020 05:16
-
-
Save misterpoloy/b08a02dc7ed4fe044a8199864eb9a82a to your computer and use it in GitHub Desktop.
Two Number Sum.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <unordered_set> | |
using namespace std; | |
// Repasado 2 veces | |
vector<int> twoNumberSum(vector<int> array, int targetSum) { | |
unordered_set<int> targets; | |
for (int i = 0; i < array.size(); i++) { | |
int y = targetSum - array[i]; | |
if (targets.find(y) != targets.end()) { | |
return vector<int> { array[i], y }; | |
} else { | |
targets.insert(array[i]); | |
} | |
} | |
return {}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2 number sums
Solution 1
Use the diffrerene from the target and your current element, use a hash function to check wether exist.
Solution 3
Use a left pointer and a right pointer, if lower increment left and if gigher decrement right.