I. Description of the topic
Second, the problem solving skills
This problem is superficially similar to the 3Sum, in fact, the same thinking and method can be used, but in this case, the time complexity is O (n^3), the space Complexity of O (1) will time out.
This problem can also be calculated after the first two number of the and, the method will be a hash table, because there may be different two numbers and for the same value, so you can consider and for the same value in a linked list, and then put the variable head in the hash table. And then follow the idea of 3Sum, but the third number here becomes the third and fourth number of the and, through the hash table can be easily found and for the fixed value of the number of the linked list, you can find the matching criteria of four number. The time complexity of this method is O (n^2), and the spatial complexity is O (n^2).
Third, instance code
classSolution { Public: vector<vector<int> >Foursum ( vector<int>&num,intTarget) { vector<vector<int> >Result;intSize = Num.size ();if(Size <4) {returnResult; }//Sort the arraySort (Num.begin (), Num.end ()); for(intIndex_first =0; Index_first < (Size-3); index_first++) {intfirst = Num[index_first];if((Index_first! =0) && (Num[index_first-1] = = Num[index_first]) {Continue; } for(intIndex_second = Index_first +1; Index_second < (Size-2); index_second++) {if((Index_second! = (Index_first +1)) && (Num[index_second-1] = = Num[index_second]) {Continue; }intSecond = Num[index_second];intIndex_third = Index_second +1;intIndex_foud = Size-1; while(Index_third < Index_foud) {intThird = Num[index_third];intFourd = Num[index_foud];intSum = first + Second + third + fourd;if(Sum = = target) { vector<int>TMP; Tmp.push_back (first); Tmp.push_back (Second); Tmp.push_back (third); Tmp.push_back (Fourd); Result.push_back (TMP); index_third++; while(Index_third <= (Size-1)) && (Num[index_third] = = Num[index_third-1]) {index_third++; } index_foud--; while((Index_foud > Index_second) && (num[index_foud] = = Num[index_foud +1]) {index_foud--; } }if(Sum < target) {index_third++; while((Index_third < Size) && (num[index_third] = = Num[index_third-1]) {index_third++; } }if(Sum > Target) {index_foud--; while((Index_foud > Index_second) && (num[index_foud] = = Num[index_foud +1]) {index_foud--; } } } } }returnResult; } };
Iv. Experience
The problem is an extension of 3Sum, but it needs to be transformed into a way to reduce computational complexity. If you just follow the 3Sum extension to do this problem, the space complexity of the algorithm will reach O (n^3), but the spatial complexity of O (1). Can be a different way of thinking, after sorting, calculate the next two number of the and, and stored with a hash table, and then change the problem to 3Ssum, but after the third number is calculated, you also need to find in the hash table and meet the condition of the third and fourth number.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode notes: 4Sum