力扣-1-两数之和

回过头来做第一题

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> map;
// 遍历数组元素,如果hashmap中存在可以凑成target的数就返回两个数的下标
// 否则就插入hashmap
for (int i = 0; i < nums.size(); ++i) {
if (map.count(target - nums[i])) {
// 这里还能这么写?!
return { i,nums[target - nums[i]] };
}
map.emplace(nums[i], i);
}
return {};
}
};

本文作者:YaosGHC

本文链接:https://www.cnblogs.com/yaocy/p/16580165.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   YaosGHC  阅读(16)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起