链接: https://oj.leetcode.com/problems/two-sum/
在一个数组中找出两个值,使得他们的和等于 target; 返回相应的下标+1
依次查找是存在值等于 target-numbers[i]
class Solution { public: vector<int> twoSum(vector<int> &numbers,int target) { map<int,int> nummap; vector<int> ans; map<int,int>::iterator iter; for(int i=0;i<numbers.size();i++) { iter=nummap.find(target-numbers[i]); if(iter!=nummap.end()) { ans.push_back(iter->second); ans.push_back(i+1); break; } else { nummap[numbers[i]]=i+1; } } return ans; } };