[Algorithms] LeetCode 1. 两数之和
Categories Algorithms HashTable
Tags
来源:代码随想录
迭代器用法
要保存下标,所以用map。注意迭代器用法。
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> map;
for (int i = 0; i < nums.size(); i++) {
auto iter = map.find(target - nums[i]);
if (iter != map.end()) {
return {i, iter->second};
}
map.insert({nums[i], i});
}
return {};
}
};
Page views: Loading... · Visitors: Loading...
Except where otherwise noted, original content on this site is dedicated to the public domain under CC0 1.0.
Powered by Hexo & Theme mdsuper
沪ICP备2026040813号