-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum2.java
More file actions
35 lines (32 loc) · 742 Bytes
/
TwoSum2.java
File metadata and controls
35 lines (32 loc) · 742 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
35
package TwoPointers;
/*
167. Two Sum II - Input Array Is Sorted
Time Complexity: O(n)
Space: O(N)
*/
public class TwoSum2 {
public static int[] twoSum2(int[] numbers, int target) {
int l = 0;
int [] res = new int[2];
int r = numbers.length -1;
if(numbers.length==2){
res[0]=1;
res[1]=2;
return res;
}
while(l<r){
if(numbers[l] + numbers[r] > target){
r--;
}
else if(numbers[l]+ numbers[r]<target){
l++;
}
else {
res[0]= l+1;
res[1] = r+1;
break;
}
}
return res;
}
}