Feel the need to re-brush the problem, for the future to find a job to prepare, choose Leetcode+topcoder on the Data science tutorials,
Strive to start every night 10:00 brush together, review the relevant knowledge points.
-------------------------------------------------------Split Line-------------------------------------------------------------- ---------
The Sum of
Given an array of integers, find the numbers such that they add up to a specific target number.
The function twosum should return indices of the numbers such that they add up to the target,
Where Index1 must is less than INDEX2. Please note that your returned answers (both Index1 and INDEX2) is not zero-based.
You may assume this each input would has exactly one solution.
Input:numbers={2, 7, one, A, target=9
Output:index1=1, index2=2
"Test Instructions": This is meant to be given a number, and then give you a target number, let you find in the given number of the two number, the sum of two numbers equals to the target number. Note that the given numbers are unordered.
"Mental Journey": see this problem, simply think about, you know O (n*2) traversal must be timed out. So further thinking, feel particularly like hash, but lazy to achieve hash ...
Just start thinking, there's no other way. Feel this disorderly number, sorted will be better to deal with a little. Just start thinking about the sort, and find out that it's better to deal with the sequence.
We use two subscripts, one pointing to start beg, one pointing at the end, and we're going to compare target minus the number pointed to by beg, and the number that the end points to. if Target-beg = = end, it is found;
If Target-beg > End, beg++, if Target-beg < end, End--。 In this case, the complexity of Time is O (N*logn). I felt almost acceptable, and I handed it in, AC.
In order to learn knowledge, AC is not an end, I looked at the official solution:
O(n2) Runtime,
O(1) Space–brute Force:
The brute force approach are simple. Loop through each element x and the find if there is another value, the Equals to target–x.
As finding another value requires looping through the rest of the array, its runtime complexity is O(n2).
O(n) Runtime,
O(n) Space–hash table:
We could reduce the runtime complexity of looking up a value to O(1) using a hash map, maps a value to it in Dex.
--The best answer is Hash,o (n).
---------------------------------------------------------is the dividing line again---------------------------------------------------------- ------------
Attached code:
(1) This is O (n*logn) sort + end-to-end subscript:
1 /**2 * Note:the returned array must is malloced, assume caller calls free ().3 */4 structNode {5 intnum;6 intLocal;7 };8 9 intcmpConst voidAConst void*b) {Ten structNode * AA = (structNode *) A; One structNode * BB = (structNode *) b; A if(Aa->num = = bb->num) { - returnAa->local-bb->Local; -}Else{ the returnAa->num-bb->num; - } - } - int* Twosum (int* Nums,intNumssize,inttarget) { + - structnode* A = (structnode*)malloc(Numssize* (sizeof(structnode )); + int* Vis = (int*)malloc(2*sizeof(int)); A intBeg =0, end = numssize-1, I; at for(i =0; i < numssize; i++) { -A[i].num =Nums[i]; -A[i].local = i+1; - } -Qsort (A,numssize,sizeof(a[0]), CMP); - for(; Beg <end;) { in intAns = target-A[beg].num; - while(1){ to if(A[end].num = =ans) { + if(A[beg].local <a[end].local) { -vis[0] =a[beg].local; thevis[1] =a[end].local; *}Else { $vis[0] =a[end].local;Panax Notoginsengvis[1] =a[beg].local; - } the returnVis; +}Else if(A[end].num <ans) { Abeg++; the Break; +}Else { -end--; $ } $ } - } - Free(a); the}
(2) using a map of C + + to simulate hash:
1 classSolution {2 Public:3vector<int> Twosum (vector<int>& Nums,inttarget) {4map<int,int>A;5 intI,len;6Len =nums.size ();7 for(i =0; i < Len; i++) {8 intAns = target-Nums[i];9 if(A.count (Nums[i])) {Tenvector<int>Res; One intAns1,ans2; AANS1 = i+1; -Ans2 =A[nums[i]]; - if(Ans1 >ans2) { the Res.push_back (ANS2); - Res.push_back (ans1); - returnRes; -}Else { + Res.push_back (ans1); - Res.push_back (ANS2); + returnRes; A } at } -A[ans] = i+1; - } - } -};
Brush Leetcode1-two Sum Together