传送门
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> res;
map<int,int> m;
for(int i=0;i<nums.size();i++){
m[nums[i]]=i;
}
for(int i=0;i<nums.size();i++){
if(m.find(target-nums[i])!=m.end()&&m[target-nums[i]]!=i){
res.push_back(i);
res.push_back(m[target-nums[i]]);
break;
}
}
return res;
}
};
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> m;
for (int i = 0; i < nums.size(); ++i) {
if (m.count(target - nums[i])) {
return {m[target - nums[i]], i};
}
m[nums[i]] = i;
}
return {};
}
};