题解 | #两数之和#
两数之和
https://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f
#include <unordered_map> 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; unordered_map<int, int> unMap; for(int i = 0; i<numbers.size(); i++) { if(unMap.count(numbers[i]) == 0) { unMap[target-numbers[i]] = i+1; } else { res.push_back(unMap[numbers[i]]); res.push_back(i+1); } } return res; } };
刚刚看的散列表,浅浅的试一下👻
#刷题#