注意用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> results; map<int,int> mp; for(int i = 0 ;i < numbers.size();i++){ mp[numbers[i]] = i; } for(int i = 0; i< numbers.size();i++){ if(mp.find(target-numbers[i])!=mp.end() && i != mp[target-numbers[i]]){ results.push_back(i+1); results.push_back(mp[target-numbers[i]]+1); break; } } return results; } };