Why 'time limit exceeded'? (C++)


  • 0
    Z
    class Solution {
    

    public:
    void twoSum(vector<int>& nums, int target, int L, vector<vector<int>>& res)
    {
    unordered_map<int, int> tmp;
    vector<int> res1;
    for (int i = L+1; i<nums.size(); i++)
    {
    int T = target - nums[i];
    if (tmp.find(T) != tmp.end())
    {

    			if ((L+1)>=(tmp[T])) continue;
    			else
    			{
    				res.push_back(vector < int > {L + 1, tmp[T], i + 1});
    				cout << L + 1 << " " << tmp[T] << " " << i + 1 << endl;
    			}
    		}
    		tmp[nums[i]] = i + 1;         
    	}
    }
    vector<vector<int>> threeSum(vector<int>& nums)
    {
    	int n = nums.size(); int T;
    	vector<vector<int>> res;
    	for (int i = 0; i < n; i++)
    	{
    		T = -nums[i];
    		twoSum(nums, T, i, res);
    	}
    	return res;
    }
    

    };


Log in to reply
 

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