class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int,int> um; vector<int> res; for(int i=0;i<nums.size();i++){ int tem=target-nums[i]; if(um.count(tem)){ res.push_back(um[tem]); res.push_back(i); return res; } um[nums[i]]=i; } } };
边判断边记录,不需要考虑重复。O(n)