[Java] Why was my answer not accepted?


  • 0
    B
    public class twoSumSolution 
    public int[] twoSum(int[] numbers, int target) 
    {
    	int[] arrayAnswer = new int[2];
    	for(int a=0; a < numbers.length; a++)
    	{
    		for(int b=a+1; b<numbers.length; b++)
    		{
    			if((numbers[a] + numbers[b]) == target)
    			{
    				arrayAnswer[0] = a;
    				arrayAnswer[1] = b;
    			}
    		}
    	}
    	System.out.println("index1=" + arrayAnswer[0] + ", index2=" + arrayAnswer[1]);
    	return arrayAnswer;
    }
    }
    

    Here's my test code

     public static void main(String[] args)
    {
    	twoSumSolution findTwoSum = new twoSumSolution();
    	int[] numbers = {4, 9, 10, 11, 12, 14, 15};
    	int target = 25;
    	int[] returnValues = new int[2];
    	returnValues = findTwoSum.twoSum(numbers, target);
    }
    

    Thanks for any feedback, and Merry Christmas, all!


  • 0
    K

    cost time too long


  • 0
    E

    Is it show time out? Try reduce complexity to O(n)


  • 0
    Y

    By how? Except for using hashmap.


Log in to reply
 

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