Original title Address
Method I: Secondary data structure
Use a map to save the values and locations encountered, the most intuitive idea.
Traverse numbers, for each element Numbers[i]:
If Target-numbers[i] is found in the map, the two numbers are found and the algorithm ends
Otherwise, Numbers[i] and location I are added to the map
Time complexity O (n), spatial complexity O (n)
1vector<int> Twosum (vector<int> &numbers,inttarget) {2map<int,int>record;3vector<int>Res;4 intSize =numbers.size ();5 6 for(inti =0; i < size; i++) {7 if(Record.find (target-numbers[i])! =Record.end ()) {8 //if it can be found, then the number in the map must be the smallest, put in front9Res.push_back (Record[target-numbers[i]) +1);TenRes.push_back (i +1); One Break; A } - Else -Record.insert (pair<int,int>(Numbers[i], i)); the } - - returnRes; -}
Method II: Double-pointer method
1. Sort the array first
2. Use two pointers (head and tail) to point to the first and last element of the array, respectively
3. Examine the size relationship of the elements referred to by the two pointers:
If head + tail = target, the description finds the two numbers, the algorithm ends
If head + Tail < Target,head move Right one element
If head + tail > Target,tail Move left one element
4. Return 3
The above algorithm can guarantee that the solution will not be omitted when moving head and tail, which proves as follows:
Assume that there is a number[I]+ Number[J]=Target (i < j). Under what circumstances can head move to the right of I? Assuming the head has moved to the right of I, head > I and Numbers[Head]> Numbers[I], according to the algorithm rules, only if the numbers[Head]+ numbers[Tail]< target, head moves right, so numbers[Tail]< Target-numbers[Head]< Target-numbers[I]= Numbers[J], meaning that the tail must appear on the left side of J. Under what circumstances could the tail move to the left of J? In the same vein, it can only happen if the head is on the right side of I. Since the first head is on the left side of I, tail on the right side of J, the algorithm moves only head or tail at a time, so it is not possible to move the head to the right of I and tail to the left of J while the algorithm moves at some point. The proof is over.
The premise of Method II is that the array must be ordered, but once the array is sorted, the position information of the original element is chaotic, how to find the original corresponding position? Need to use a secondary data structure to save the original location, Ah, feeling suddenly low a big cut.
Time complexity O (n), spatial complexity O (n)
1 BOOLLESSP (pair<int,int> &a, pair<int,int> &b) {2 returnA.first <B.first;3 }4 5vector<int> Twosum (vector<int> &numbers,inttarget) {6vector<pair<int,int> >record;7vector<int>Res;8 intR = numbers.size ()-1;9 intL =0;Ten One //additional data structures are required to save location information A for(inti =0; I <= R; i++) -Record.push_back (pair<int,int>(Numbers[i], i)); - the sort (Record.begin (), Record.end (), LESSP); - while(L <r) { - intsum = Record[l].first +Record[r].first; - if(Sum = =target) { +Res.push_back (Record[l].second +1);//The serial number starts at 1. -Res.push_back (Record[r].second +1);//The serial number starts at 1. + //results must appear in order from small to large A sort (Res.begin (), Res.end ()); at Break; - } - Else if(Sum >target) -r--; - Else -l++; in } - to returnRes; +}
Leetcode#1 the Sum