标签:
原题: https://leetcode.com/problems/two-sum/
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
思路: 队列,flag,hash
本题可以划分为两块儿:
注意到,本题一定有解,而且有唯一解,这就大大简化了思路。
对于1,计算复杂度为O(n)的方法是,先排序,然后队头队尾都有个flag,当其和大于target,说明大了,把队尾flag前移,其和小于target,说明小了,把队头flag后移,直到等于的情况,这样就找到了那两个数。
对于2,显然需要记住每个数的index,方法就是用hash函数,用map来实现。
特殊情况:
队列里有两个或更多相同数字。如果相同数字个数大于2,说明不是要找的数,否则违背唯一性原则,所以不用管;如果要找的数恰好是两个相同的,会存在hash冲突,我用了个笨法,搞了两个map...
自己a过去的代码:
1 #include <map> 2 #include <algorithom> 3 class Solution { 4 public: 5 6 vector<int> twoSum(vector<int>& nums, int target) { 7 vector<int> result; 8 map <int,int> name,name2; 9 vector<int>::iterator it,it2; 10 for(it=nums.begin();it!=nums.end();++it){ 11 if (name.find(*it) == name.end()) 12 name[*it]=it-nums.begin()+1; 13 else 14 name2[*it]=it-nums.begin()+1; 15 } 16 sort(nums.begin(),nums.end()); 17 it=nums.begin(); 18 it2=nums.end()-1; 19 while(it!=it2){ 20 if (*it+*it2>target) 21 it2--; 22 else if (*it+*it2<target) 23 it++; 24 else if (*it+*it2==target){ 25 if(*it!=*it2){ 26 result.push_back(min(name[*it],name[*it2])); 27 result.push_back(max(name[*it],name[*it2])); 28 } 29 else{ 30 result.push_back(min(name[*it],name2[*it])); 31 result.push_back(max(name[*it],name2[*it])); 32 } 33 return result; 34 } 35 } 36 } 37 };
标签:
原文地址:http://www.cnblogs.com/aezero/p/4461611.html