Java solution failed at last case - why it will show "Time Limit Exceeded"


  • 0
    J
    public static int[] twoSum(int[] numbers, int target)
    {
    	int left = 0;
    	int right = 0;
    	int[] index = { 0, 0 };
    	for (int n = 0; n < numbers.length - 1; n++)
    	{
    		for (int i = n + 1; i < numbers.length; i++)
    		{
    			if(numbers[n] + numbers[i] ==  target)
    			{
    				index[0] = n + 1;
    				index[1] = i + 1;
    			}
    		}
    	}
    
    	return index;
    }
    

    //I think it is O(n^2) and it is the brute way


  • 2
    B

    An O(n^2) solution cannot pass the test case...


Log in to reply
 

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