leetcode_1-两数之和
时间:2021-04-13 12:55:20
收藏:0
阅读:0
题目
代码
#include <unordered_map>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int> &nums, int target) {
vector<int> res_vec;
unordered_map<int, int> value_index_map;
for (int i = 0; i < nums.size(); ++i) {
auto iter = value_index_map.find(target - nums[i]);
if (iter != value_index_map.end()) {
res_vec.emplace_back(iter->second);
res_vec.emplace_back(i);
return res_vec;
}
value_index_map.emplace(nums[i], i);
}
return res_vec;
}
};
评论(0)