和为S的两个数字
代码实现
package 剑指offer;
import java.util.ArrayList;
/**
* @author WangXiaoeZhe
* @Date: Created in 2019/11/22 16:09
* @description:
*/
public class Main9 {
public static void main(String[] args) {
}
public ArrayList<Integer> FindNumbersWithSum(int[] array, int sum) {
ArrayList<Integer>list=new ArrayList<>();
if (array == null || array.length < 2) {
return list;
}
int i = 0, j = array.length - 1;
while (i < j) {
if (array[i] + array[j] == sum) {
list.add(array[i]);
list.add(array[j]);
} else if (array[i] + array[j] > sum) {
j--;
}else {
i++;
}
}
return list;
}
}