Your solutions are too long


  • 0
    T
    class Solution {
    public:
        vector<vector<int>> threeSum(vector<int>& nums) {
            sort(nums.begin(), nums.end());
            vector<vector<int>> r;
            for(int i=0; i<nums.size(); i++)
                if(!i || nums[i] != nums[i-1])
                    for(int j=i+1, k=nums.size()-1; j<k; j++)
                    {
                        while(j < k && nums[i] + nums[j] + nums[k] > 0)
                            k--;
                        if(j < k && nums[i] + nums[j] + nums[k] == 0 &&
                        (r.empty() || r.back()[2] != nums[k] || r.back()[1] != nums[j]))
                            r.push_back({nums[i], nums[j], nums[k]});
                    }
            return r;
        }
    };

Log in to reply
 

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