Please Help Soln. Not Working


  • 0
    N

    can someone please explain why my solution isnt working. i can't seem to understand

    class Solution {
    public:
        vector<vector<int>> threeSum(vector<int>& nums) {
            if(nums.empty())
                return {};
            set<vector<int>> v;
            sort(nums.begin(), nums.end());
            for(int i=0; i<nums.size()-1; i++)
            {
                int target = -1*nums[i];
                int l = i+1;
                int r = nums.size()-1;
                while(l < r)
                {
                    int sum = nums[l]+nums[r];
                    if(target == sum)
                    {
                        s.insert({nums[i],nums[l],nums[r]});
                        l++;
                        r--;
                    }
                    else if(target > sum)
                        r--;
                    else if(target < sum)
                        l++;
                }
            }
            vector<vector<int>> v(s.begin(), s.end());
            return v;
        }
    };
    

Log in to reply
 

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