解题思路:
1. 先找到最小值所在位置;
2. 以循环交界处(最小值前一位)为分界,分两段做二分查找
public class Solution {
public int binarySearch(int[] nums, int beg, int end, int target){
if (beg == end){
if (nums[beg] == target){
return beg;
}else{
return -1;
}
}else{
int mid = beg + (end - beg) / 2;
if (nums[mid] < target){
beg = mid + 1;
}else{
end = mid;
}
return binarySearch(nums, beg, end, target);
}
}
public int search(int[] nums, int target) {
int beg = 0, end = nums.length - 1, mid;
//先寻找最小值的位置
while (beg < end){
mid = beg + (end - beg) / 2;
if (nums[mid] > nums[end])
beg = mid + 1;
else
end = mid;
}
//nums_min = nums[beg],分两段二分查找
if (beg > 0){
int ret = binarySearch(nums, 0, beg - 1, target);
if (ret != -1)
return ret;
}
return binarySearch(nums, beg, nums.length - 1, target);
}
}