题解 | #两数之和#
两数之和
https://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f
#include <unordered_map> class Solution { public: vector<int> twoSum(vector<int>& numbers, int target) { vector<int> res; unordered_map<int, int> hash; for(int i=0;i<numbers.size();++i) { int tmp=target-numbers[i]; if(hash.find(tmp)==hash.end()) hash[numbers[i]]=i; else { res.push_back(hash[tmp]+1); res.push_back(i+1); } } return res; } };