class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) { // 定义返回格式
unordered_map<int, int> hashtable; // 定义字典
for (int i=0; i < nums.size(); ++i){
auto it = hashtable.find(target - nums[i]); // 查找value(index)
if (it != hashtable.end()) { // 判断value是否在字典中
return {it->second, i}; // it->first和it->second, 分别得到key和value
}
hashtable[nums[i]] = i;
}
return {};
}
};
版权声明:本文为wdh315172原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。