forked from JadeZYX/Java_LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP0167_TwoSum2.java
More file actions
68 lines (67 loc) · 1.88 KB
/
Copy pathP0167_TwoSum2.java
File metadata and controls
68 lines (67 loc) · 1.88 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
66
67
68
import java.util.HashMap;
public class P0167_TwoSum2 {
//two pointers
public int[] twoSum2(int[]nums,int target){
int[]res=new int[2];
int left=0;
int right=nums.length-1;
while(left<right){
if(nums[left]+nums[right]==target){
res[0]=left+1;
res[1]=right+1;
return res; //break;
}
if(nums[left]+nums[right]>target){
right--;
}
else{
left++;
}
}
return null;
}
//hashmap
public int[] twoSum2_1(int[]numbers,int target){
int[] res=new int[2];
HashMap<Integer,Integer> map=new HashMap<>();
for(int i=0;i<numbers.length;i++){
if(map.containsKey((target-numbers[i]))){
res[0]=map.get(target-numbers[i])+1;
res[1]=i+1;
return res;
}
map.put(numbers[i],i);
}
return null;
}
//binary search
public int[] twoSum_2(int[]numbers,int target){
int[]res=new int[2];
for(int i=0;i<numbers.length;i++){
int pos=binarySearch(numbers,i+1,numbers.length-1,target-numbers[i]);
if(pos!=-1){
res[0]=i+1;
res[1]=pos+1;
return res;
}
}
return null;
}
public int binarySearch(int[]numbers,int left,int right,int target){
while(left<=right){
int mid=left+(right-left)/2;
if(target==numbers[mid]){
return mid;
}
else if(numbers[mid]>target){
right=mid-1;
}
else{
left=mid+1;
}
}
return -1;
}
}
//P0167_TwoSum2 p167=new P0167_TwoSum2();
//System.out.println(p167.twoSum(new int[]{2,7,11,15},9));