[Array] LEETCODE15 algorithm to find an array of three numbers and all combinations of 0

// Solution a, map the array is divided into n, 0, p three groups, looking 9 (0,0,0), (n, 0, p) (n, n, p) and (p, p, n) of combination, but because not be repeated, and finally a combination of two so much trouble, the complexity is O (n ^ 2), traversed once, the last two TLE Case 
class Solution {
 public : 
    Vector <Vector < int >> threeSum (Vector < int > & the nums) { 
        Map < int , int > NE; 
        Map < int , int > PO;
         int NNE = 0 , NPO = 0 , ZERO = 0 ; 
        Vector <Vector < int >> answer;
         for(int i=0;i<nums.size();i++){
            if(nums[i]>0){
                po[nums[i]]++;
                npo++;
            }
            else if(nums[i]<0){
                ne[nums[i]]++;
                nne++;
            }
            else zero++;
        }
        if(zero>=3){
            vector<int> v;
            v.push_back(0);
            v.push_back(0);
            v.push_back(0);
            answer.push_back(v);
        }
        if(nne&&npo){
            if(zero){
                for(map<int,int>::iterator it=ne.begin();it!=ne.end();it++){
                    if(po[-it->first]){
                        vector<int> v;
                        v.push_back(it->first);
                        v.push_back(0);
                        v.push_back(-it->first);
                        answer.push_back(v);
                    }
                    else po.erase(-it->first);
                }
            }
            if(nne>=2){
                for(map<int,int>::iterator i=ne.begin();i!=ne.end();i++){
                    for(map<int,int>::iterator j=i;j!=ne.end();j++){
                        if(i->first==j->first && i->second<2) continue;
                        else{
                            int left= -((i->first)+(j->first));
                            if(po[left]){
                                vector<int> v;
                                v.push_back(i->first);
                                v.push_back(j->first);
                                v.push_back(left);
                                answer.push_back(v);
                            }
                            else po.erase(left);
                        }
                    }
                }
            }
            if(npo>=2){
                for(map<int,int>::iterator i=po.begin();i!=po.end();i++){
                    for(map<int,int>::iterator j=i;j!=po.end();j++){
                        if(i->first==j->first && i->second<2) continue;
                        else{
                            int left=-(i->first+j->first);
                            if(ne[left]){
                                vector<int> v;
                                v.push_back(i->first);
                                v.push_back(j->first);
                                v.push_back(left);
                                answer.push_back(v);
                            }
                            else ne.erase(left);
                        }
                    }
                }
            }
        }

        return answer;
    }
};
// Method 2, each have a number, look for the other two the number of qualifying. The array sorted, you can use the dichotomy of thinking, to avoid the enumeration of all combinations of the other two numbers. Sorting O (nlogn), the latter traversing about O (n ^ 2), 
but is a constant coefficient, and the map is not cost
class Solution { public : Vector <Vector < int >> threeSum (Vector < int > & the nums) { Sort (nums.begin (), nums.end ()); Vector <Vector < int >> Result; for ( int I = 0 ; I <nums.size (); I ++ ) { IF (the nums [I]> 0 ) BREAK ; IF (I> 0 && the nums [I] == the nums [I-1]) continue; int left=i+1,right=nums.size()-1; while(left<right){ int sum=nums[left]+nums[right]+nums[i]; if(sum>0){ right--; } else if(sum<0){ left++; } else{ result.push_back({nums[i],nums[left],nums[right]}); int l=nums[left]; int r=nums[right]; while(left<right&&nums[left]==l) left++; while(right>left&&nums[right]==r) right--; } } } return result; } };

 

Guess you like

Origin www.cnblogs.com/rarecu/p/11520902.html