And S is the number of two - prove safety offer

Title Description

An incrementing input and a digital sorted array S, find the two numbers in the array, and so that they are exactly S, and if a plurality of digits equal to S, the product of the output of the minimum number of two.

Output Description:

Corresponding to each test case, the output of two numbers, the first small output. 

Ideas: increasing sequence about Squeeze law
import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> FindNumbersWithSum(int [] array,int sum) {
        ArrayList<Integer>arrayList=new ArrayList<>();
        int i =0,j =array.length-1;
        while (i < j){
            int tem=array[i] + array[j];
            if(tem == sum){
                arrayList.add(array[i]);
                arrayList.add(array[j]);
                break;
            }else if(tem > sum){
                j --;
            }else {
                i ++;
            }
        }
        return  arrayList;
    }
}

 

Guess you like

Origin www.cnblogs.com/nlw-blog/p/12455291.html
Recommended