//Time:O(n),Space:O(n)
//语言:C++11
class Solution
{
public:
vector<int> twoSum(vector<int>& nums, int target)
{
vector<int> res={-1,-1};
unordered_map<int,int> hash_map;
for(int i=0;i<nums.size();i++)
{
int another=target-nums[i];
if(hash_map.count(another))
{
res=vector<int>({hash_map[another],i});
break;
}
hash_map[nums[i]]=i;
}
return res;
}
};
//换一种语言来实现,用经典C++语言来实现
class Solution
{
public:
vector<int> twoSum(vector<int>& nums, int target)
{
vector<int> ret;
map<int,int> hash_map;
for(int i=0;i<nums.size();i++)
{
int another=target-nums[i];
if(hash_map.count(another))
{
ret.push_back(hash_map[another]);
ret.push_back(i);
break;
}
hash_map[nums[i]]=i;
}
return ret;
}
};