标签:leetcode
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector<int> twoSum1(2); map<int,int> mValueIdex; map<int,int>::iterator it; bool flag=0; for(int i=0;i<nums.size();i++) { //两个一样. if(nums[i]==target/2) { if(flag==0) { mValueIdex[nums[i]]=i; flag=1; } else { twoSum1[0]=mValueIdex[nums[i]]+1; twoSum1[1]=i+1; return twoSum1; //0 0 1)? } }else { mValueIdex[nums[i]]=i; it=mValueIdex.find(target-nums[i]); if(it!=mValueIdex.end()) { twoSum1[1]=mValueIdex[nums[i]]+1;//i是最后出现的 twoSum1[0]=mValueIdex[target-nums[i]]+1; return twoSum1; //0 0 1)? } } } } };
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:leetcode
原文地址:http://blog.csdn.net/q286989429/article/details/47322055