forked from caofanCPU/JavaBooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT42.java
More file actions
34 lines (31 loc) · 958 Bytes
/
T42.java
File metadata and controls
34 lines (31 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package web; /**
* @program LeetNiu
* @description: 和为S的两个数字
* @author: mf
* @create: 2020/01/15 10:48
*/
import java.util.ArrayList;
/**
* 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。
*/
public class T42 {
public ArrayList<Integer> FindNumbersWithSum(int [] array, int sum) {
int start = 0, end = array.length - 1;
ArrayList<Integer> list = new ArrayList<>();
while (start < end) {
int count = array[start] + array[end];
if (count < sum) {
start++;
}
if (count == sum) {
list.add(array[start]);
list.add(array[end]);
return list;
}
if (count > sum) {
end--;
}
}
return list;
}
}