class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int>answer;
map<int,int>Q;
for(int i=0;i<nums.size();i++){
Q.insert(pair<int, int>(nums[i], i));
}
for(int i=0;i<nums.size();i++){
// cout<<nums[i]<<endl;
if(Q.count(target-nums[i]))
{
// cout<<target<<endl;
// cout<<"yes "<<Q[target-nums[i]]<<endl;
if(i!=Q[target-nums[i]]){
answer.push_back(min(Q[target-nums[i]],i));
answer.push_back(max(Q[target-nums[i]],i));
return answer;
}
}
}
return answer;
}
};
Leetcode: Two Sum
最新推荐文章于 2019-01-03 13:40:30 发布