forked from geekxh/hello-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
37 lines (30 loc) · 828 Bytes
/
Solution.java
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
35
36
37
/**
* @author mcrwayfun
* @version v1.0
* @date Created in 2019/02/02
* @description
*/
public class Solution {
public ArrayList<Integer> FindNumbersWithSum(int[] array, int sum) {
ArrayList<Integer> reList = new ArrayList<>();
if (array == null || array.length < 2 || sum <= array[0]) {
return reList;
}
int start = 0;
int end = array.length - 1;
while (start < end) {
int curSum = array[start] + array[end];
if (curSum == sum) {
reList.add(array[start]);
reList.add(array[end]);
return reList;
} else if (curSum < sum) {
start++;
} else {
end--;
}
}
// 查无
return reList;
}
}