-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path42.java
More file actions
25 lines (24 loc) · 692 Bytes
/
42.java
File metadata and controls
25 lines (24 loc) · 692 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
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> FindNumbersWithSum(int [] array,int sum) {
ArrayList<Integer> result=new ArrayList<Integer>();
if(array==null || array.length<=1) return result;
int n=array.length-1;
int low=0,high=n;
while(low<high)
{
int curSum=array[low]+array[high];
if(curSum>sum)
high--;
else if(curSum<sum)
low++;
else
{
result.add(array[low]);
result.add(array[high]);
return result;
}
}
return result;
}
}