-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotatedArray.java
More file actions
54 lines (43 loc) · 1.11 KB
/
rotatedArray.java
File metadata and controls
54 lines (43 loc) · 1.11 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
class solution{
public static void main(String[] args){
int[] nums = new int[]{0,1,2,3,4,5,6,7};
int res = search(nums,4);
System.out.println(res);
}
static int search(int[] nums, int target) {
int pivot = findPivot(nums, 0, nums.length - 1);
if(nums[pivot] == target)
return pivot;
int leftRes = binarySearch(nums,pivot, nums.length - 1, target);
int rightres = binarySearch(nums,0,pivot - 1, target);
if(leftRes != -1)
return leftRes;
return rightres;
}
static int findPivot(int[] nums, int l, int r){
int left = l;
int right = r;
while(left <= right){
int mid = left + (right-left) / 2;
if(nums[mid] > nums[right])
left = mid + 1;
else
right = mid - 1;
}
return left;
}
static int binarySearch(int[] nums, int l, int r, int k){
int left = l;
int right = r;
while(left <= right){
int mid = left + (right - left) / 2;
if(nums[mid] == k)
return mid;
if(nums[mid] > k)
right = mid - 1;
else
left = mid + 1;
}
return -1;
}
}