We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 01a274c commit 01e2d5cCopy full SHA for 01e2d5c
1 file changed
swordoffer/TwoNumbersSumInSortedArray.java
@@ -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
25
26
+}
0 commit comments