Can anyone tell me while I get "Time limit exceed " in O(n^2)


  • 0
    Y

    class Solution {
    public:
    vector<vector<int>> threeSum(vector<int>& nums) {

        set<vector<int>> array;
        vector<vector<int>> array2;
        if(nums.size()<3){
            return array2;
            
        }
        sort(nums.begin(),nums.end());
        for(int i=0;i<nums.size()-2;i++){
            if(nums[i]>0){
                break;
            }else{
                
            int l = i+1;
            int r = nums.size()-1;
            while(l<r){
                if(nums[i]+nums[l]+nums[r]==0){
                    array.insert(vector<int> {nums[i],nums[l],nums[r]});
                    l++;
                    r--;
                 }else if(nums[i]+nums[l]+nums[r]<0){
                    l++;
                 }else{
                    r--;
                 }
                }
            }
        }
        
        set<vector<int>>::iterator it;
        for(it=array.begin();it!=array.end();it++){
            array2.push_back(*it);
        }
        return array2;
        
        
    }
    

    };


  • 0
    L

    The first loop need avoid duplicate number repeating looping .


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.