Skip to content

Commit 01e2d5c

Browse files
committed
剑指Offer,和为S的两个数字
1 parent 01a274c commit 01e2d5c

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 剑指Offer,和为S的两个数字
3+
*/
4+
import java.util.ArrayList;
5+
public class TwoNumbersSumInSortedArray {
6+
7+
public ArrayList<Integer> FindNumbersWithSum(int [] array,int sum) {
8+
9+
ArrayList<Integer> result = new ArrayList<>();
10+
11+
int i = 0;
12+
int j = array.length - 1;
13+
while (i < j) {
14+
if (array[i] + array[j] == sum) {
15+
result.add(array[i]);
16+
result.add(array[j]);
17+
return result;
18+
}else if (array[i] + array[j] < sum) {
19+
i++;
20+
}else{
21+
j--;
22+
}
23+
}
24+
return result;
25+
}
26+
}

0 commit comments

Comments
 (0)