Concise Java solution


  • 0
    L
      public int[] twoSum(int[] numbers, int target) {
            if(numbers==null || numbers.length==0) {
                return new int[0];
            }
            
            for(int i=0,j=numbers.length-1;j>i;) {
                if(numbers[i] + numbers[j]==target) {
                    return new int[]{i+1,j+1};
                }
                else if(numbers[i] + numbers[j]>target) {
                    j--;
                }
                else {
                    i++;
                }
            }
            
            return new int[0];
        }
    

Log in to reply
 

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