题解 | #两数之和#
两数之和
http://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f
class Solution {
private:
map<int,int> mydict;
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;
for(int i=0;i<numbers.size();i++){
int sub = target-numbers[i];
if(mydict.find(sub)!=mydict.end()){
res.push_back(mydict[sub]);
res.push_back(i+1);
}else{
mydict[numbers[i]] = i+1;
}
}
return res;
}
};