利用哈希表存储数组并查找target值减去当前遍历到的值是否在哈希表中
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]);
if (it != hashTable.end()) {
return {it->second, i};
}
hashTable[nums[i]] = i;
}
return {};
}
};