-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main57.java
35 lines (33 loc) · 938 Bytes
/
Main57.java
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 JZOffer2;
public class Main57 {
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
if(n==2){
if(nums[0] + nums[1] == target){
return new int[]{nums[0], nums[1]};
}else{
return new int[]{};
}
}
for(int i = 0; i < n; i++){
if(i+1 <= n-1 &&binarySearch(nums,i+1, n-1, target - nums[i])){
return new int[]{nums[i], target - nums[i]};
}
}
return new int[]{};
}
boolean binarySearch(int[] nums, int left, int right, int num){
while (left <= right){
int mid = (left+right) / 2;
if(nums[mid] == num){
return true;
}
else if(nums[mid] > num){
right = mid - 1;
}else{
left = mid + 1;
}
}
return false;
}
}