标签:inline solution order amp vector count mat map public
创建一个哈希表,对于每一个 \(nums[i]\),我们首先查询哈希表中是否存在 \(target - nums[i]\),然后将 \(nums[i]\) 插入到哈希表中,即可保证不会让 \(nums[i]\) 和自己匹配。
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int,int> mp;
for(int i = 0; i < nums.size(); i++)
{
int x = target - nums[i];
if(mp.count(x)) return {mp[x], i};
mp[nums[i]] = i;
}
return {};
}
};
标签:inline solution order amp vector count mat map public
原文地址:https://www.cnblogs.com/fxh0707/p/14921991.html