JAVA-----------Easy Version To UnderStand!!!!!!!!!


  • 0
    H
    public static int[] twoSum(int[] numbers, int target){
    	  if(numbers==null||numbers.length==0)
    		  return new int[0];
    	  int[] result=new int[2];
    	  int len=numbers.length,low=0,high=len-1;
    	  while(low<high){
    		  if(numbers[low]+numbers[high]==target){
    			  result[0]=low+1;
    			  result[1]=high+1;
    			  return result;
    		  }
    		  else if(numbers[low]+numbers[high]>target)
    			  high--;
    		  else
    			  low++;
    	  }
    	  return new int[0];
      }

  • 0
    J

    Could you proof why this algorithm is correct? It's a little bit hard for me to understand why this algorithm works.


Log in to reply
 

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