TwoSum 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)?
				}
			}
		}
	}   
};

posted @ 2017-05-27 19:55  yangykaifa  阅读(171)  评论(0编辑  收藏  举报