Skip to content

Commit

Permalink
Update 01 Two Sum.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
skjha1 authored Jul 25, 2021
1 parent 131e046 commit 1cfd8c6
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Leetcode/Easy/01 Two Sum.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
// https://leetcode.com/problems/two-sum/discuss/1162422/Explained-with-pen-and-paper-and-gfg-ref


class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
//Key is the number and value is its index in the vector.
unordered_map<int,int> hash;
vector<int> res;
for (int i=0;i<nums.size();++i)
{
int numbertofind=target-nums[i];
//if numberToFind is found in map, return them
if (hash.find(numbertofind)!=hash.end())
{
res.push_back(hash[numbertofind]);
res.push_back(i);
return res;
}
hash[nums[i]]=i;
}
return res;

}
};

#include <vector>
#include <iostream>

Expand Down

0 comments on commit 1cfd8c6

Please sign in to comment.