-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoatedBinarySearch.java
More file actions
63 lines (48 loc) · 1.54 KB
/
RoatedBinarySearch.java
File metadata and controls
63 lines (48 loc) · 1.54 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
public class RoatedBinarySearch {
/*public int search(int[] nums, int target) {
return doBinarySearch(nums,0,nums.length-1,target);
}
private int doBinarySearch(int[] nums,int low,int high,int key){
if(low>high || high<low)
return -1;
int mid=low+(high-low)/2;
if(nums[mid]==key)
return mid;
if(key<nums[mid] && nums[mid+1]<nums[mid])
return doBinarySearch(nums,mid+1,high,key);
if(key>nums[mid] && nums[mid+1]>nums[mid])
return doBinarySearch(nums,mid+1,high,key);
else
return doBinarySearch(nums,low,mid-1,key);
}*/
public static int search(int[] nums, int target) {
if(nums.length==1 && target!=nums[0])
return -1;
int low=0;
int high=nums.length-1;
while(low<=high){
int mid=low+(high-low)/2;
if(nums[mid]==target)
return mid;
if(target>=nums[low])
high=mid-1;
else
low=mid+1;
}
return -1;
}
public static void main(String[] args) {
/*int[] nums={4,5,6,7,0,1,2};
int target=2;*/
/* *//*int[] nums={1,3};
int target=3;*//*
RoatedBinarySearch roatedBinarySearch=new RoatedBinarySearch();
int idx = roatedBinarySearch.search(nums, target);
System.out.println(idx);
*/
int[] nums={5,1,3};
int target=5;
int idx = search(nums, target);
System.out.println(idx);
}
}