Java solution o(n^2) time complexity.


  • -2
    B

    public class Solution {

    public int[] twoSum(int[] numbers, int target) {

            int[] result = new int[2];
            int firstNumber = numbers[0];
            int secondNumber = 0;
            
            for(int i = 1; i < numbers.length; i++){
                if(firstNumber < target){
                    secondNumber = target - firstNumber;
                    for(int j = i+1; j < numbers.length; j++){
                        if(secondNumber == numbers[j]){
                            result[0] = i;
                            result[1] = j+1;         
                    }
                }
                }
                    firstNumber = numbers[i];
            }
            
            return result;
            
            
        }
    

    }


  • 0
    V

    This showsTime Limit Exceeded,


Log in to reply
 

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