题解 | #两数之和#
两数之和
https://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f
class Solution { public: /** * * @param numbers int整型vector * @param target int整型 * @return int整型vector */ vector<int> twoSum(vector<int>& numbers, int target) { // write code here vector<int>res; int n = numbers.size(); unordered_map<int,int>mp; for(int i = 0;i<n;i++) { if(mp.find(target-numbers[i])!=mp.end()) { res.push_back(mp[target-numbers[i]]+1); res.push_back(i+1); } else{ mp[numbers[i]]=i; } } return res; } };