Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Implementation of two sum problem (hash table)
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> Result;
unordered_map<int, int> hash;
for(int i =0 ;i< nums.size(); i++)
{
int numOfComplement = target - nums[i];
if(hash.find(numOfComplement) != hash.end())
{
int temp = hash[numOfComplement];
Result.push_back(temp);
Result.push_back(i);
return Result;
}
else
hash[nums[i]] = i;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment