I don't know why it runs TLE


  • -1
    M

    class Solution {
    public:
    vector<vector<int>> threeSum(vector<int>& nums) {
    nums.erase(unique(nums.begin(),nums.end()),nums.end());
    vector<vector<int>> res;
    int left,right;
    vector<int> eachone;
    eachone.resize(3);
    if(nums.size() < 3){
    return res;
    }else{
    sort(nums.begin(),nums.end());
    for(int i = 0; i < nums.size()-2; ++i){
    left = i+1;
    right = nums.size()-1;
    while(left < right){
    if(nums[left] + nums[right] == -nums[i]){
    eachone[0] = nums[i];
    eachone[1] = nums[left];
    eachone[2] = nums[right];
    res.push_back(eachone);
    left++;
    right--;
    }else if(nums[left] + nums[right] < -nums[i]){
    left++;
    }else{
    right--;
    }
    }
    }
    }
    return res;
    }
    };


Log in to reply
 

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