Can anyone explain this code working?It is to find index of 2 elements in vector that add to produce the given target.I don't understand how STL works in this.
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int,int>m;
vector<int>v;
if(nums.size()==0)
{
return v;
}
for(int i=0;i<nums.size();i++)
{
if(m.find(nums[i])==m.end())
{
m[target-nums[i]]=i+1;
}
else
{
v.push_back(m[nums[i]]);
v.push_back(i+1);
}
}
return v;
}
};
See Question&Answers more detail:os