【LEETCODE】38、167题,Two Sum II - Input array is sorted

package y2019.Algorithm.array;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array
 * @ClassName: TwoSum2
 * @Author: xiaof
 * @Description: 167. Two Sum II - Input array is sorted
 * Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
 * The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.
 *
 * Input: numbers = [2,7,11,15], target = 9
 * Output: [1,2]
 * Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
 *
 * @Date: 2019/7/2 10:22
 @Version *: 1.0 
 * / 
public  class TwoSum2 { 

    public  int [] Solution ( int [] Numbers, int target) {
         // find two numbers is the corresponding target, and the corresponding feedback index 
        int index1, = 0, index2 = 0 ;
         // this is actually traverse, it is known that target, then every time we just get to the first data in the table, and then on the back of data in a binary search to determine whether there will be a corresponding data 
        for ( int 0 = I; I <numbers.length; ++ I) { 
            index1, = I;
             int num1 = Numbers [I];
             // binary search target 
            int targetNum = target - num1;
            int Start + = I. 1, End = numbers.length -. 1 ;
             the while (Start < End) {
                 int MID = Start + ((End - Start) >>>. 1 );
                 IF (Numbers [MID] == targetNum) { 
                    index2 = MID;
                     BREAK ; 
                } the else  IF (Numbers [MID]> targetNum) {
                     // if the target position is larger than the target data, the data in the foregoing description looking 
                    End = MID; 
                } the else {
                     //If the data is smaller than the target, then explained later, and then we have done a mid position comparison, so after a shift
                     // since mid = start + (end - start ) >>> 1; may be exactly equal to the Start 
                    Start = mid + . 1 ; 
                } 
            } 

            IF (== Start End) {
                 // If the head and tail are equal, then manually determine what 
                IF (targetNum == Numbers [Start]) { 
                    index2 = Start; 
                } 
            } 

            IF (! index2 = 0 ) {
                 BREAK ; 
            } 
        } 

        int Result [] = new new  int [2 ];
        the Result [ 0] = index1 + 1 ; 
        the Result [ 1] = index2 + 1 ;
         return the Result; 
    } 


    // Online Daniel fastest, but also accounted for the smallest memory algorithm, somewhat similar to the idea of fast discharge of 
    public  int [] twoSum ( int [] Numbers, int target) {
         int [] RES = new new  int [2 ];
         int index1, = 0 ;
         int index2 numbers.length = -. 1 ;
         the while (index2> index1,) {
             IF (Numbers [index1,] + Numbers [index2 ]> target) { 
                index2 -;
            }
            else if(numbers[index1] + numbers[index2] < target){
                index1 ++;
            }else{
                res[0] = index1+1;
                res[1] = index2+1;
                break;
            }
        }
        return res;
    }


    public static void main(String args[]) {

        int pres[] = {5,25,75};
        int target = 100;
        System.out.println(new TwoSum2().solution(pres, target));

    }

}

 

Guess you like

Origin www.cnblogs.com/cutter-point/p/11119433.html