-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtwoSumSorted.java
More file actions
65 lines (59 loc) · 1.84 KB
/
twoSumSorted.java
File metadata and controls
65 lines (59 loc) · 1.84 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.util.Arrays;
public class twoSumSorted {
public static void main(String[] args){
twoSumSorted obj = new twoSumSorted();
System.out.println(Arrays.toString(obj.twoSum(new int[]{1,2,7,9},9)));
System.out.println(Arrays.toString(obj.twoSumv2(new int[]{1,2,7,9},9)));
}
// Approach 1: two pointers
/* use two pointers.
Time complexity: O(n)
Space complexity: O(1)
* */
private int[] twoSum(int[] nums, int target){
int lo = 0, hi = nums.length-1;
while(lo < hi){
if(nums[lo] + nums[hi] == target){
return new int[] {lo+1, hi+1};
}
if(nums[lo] + nums[hi] < target){
lo ++;
}else{
hi --;
}
}
throw new IllegalArgumentException("No valid solutions");
}
// Approach 2: binary search
/* Fix the first element nums[i] and do binary search on the remaining elements.
Time complexity: lg(n-1) + lg(n-2) + ... + lg(1) ~ O(lg(n!)) ~ O(nlgn)
Space complexity: O(1)ir
* */
private int[] twoSumv2(int[] nums, int target){
for(int i = 0; i < nums.length; i++){
int j = bs(nums, i + 1, nums.length-1, target - nums[i]);
if(j != -1){
return new int[] {i+1,j+1};
}
}
throw new IllegalArgumentException("No valid solutions");
}
private int bs(int[] nums, int lo, int hi, int target){
int mid;
while(lo + 1 < hi){
mid = lo + (hi - lo)/2;
if(nums[mid] < target){
lo = mid;
}else{
hi = mid;
}
}
if(nums[lo] == target){
return lo;
}else if(nums[hi] == target){
return hi;
}else{
return -1;
}
}
}