-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSortedRotatedArraySearch.java
33 lines (32 loc) · 1.03 KB
/
SortedRotatedArraySearch.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
/*https://leetcode.com/problems/search-in-rotated-sorted-array/*/
class Solution {
public int search(int[] nums, int target) {
return binarySearch(nums, target, 0, nums.length-1);
}
public int binarySearch(int[] nums, int target, int start, int end)
{
if (start <= end)
{
int mid = (start+end)/2;
if (nums[mid] == target)
{
return mid;
}
else if (target > nums[mid])
{
if (target > nums[end] && nums[end] >= nums[mid])
return binarySearch(nums,target,start,mid-1);
else
return binarySearch(nums,target,mid+1,end);
}
else if (target < nums[mid])
{
if (target < nums[start] && nums[start] <= nums[mid])
return binarySearch(nums,target,mid+1,end);
else
return binarySearch(nums,target,start,mid-1);
}
}
return -1;
}
}