package 分治法.q33_搜索旋转排序数组;
/**
* 循环有序数组查找 二分法o(log(n))
*/
class Solution {
public int search(int[] nums, int target) {
return search(nums, 0, nums.length - 1, target);
}
private int search(int[] nums, int low, int high, int target) {
if (low > high) {
return -1;
}
int mid = (low + high) / 2;
if (nums[mid] == target) {
return mid;
}
//nums[mid] < nums[high]说明后半段有序
if (nums[mid] < nums[high]) {
//说明target在后半段
if (nums[mid] < target && target <= nums[high]) {
return search(nums, mid + 1, high, target);
}
return search(nums, low, mid - 1, target);
} else {
//后半段无序前半段有序,target在前半段
if (nums[low] <= target && target < nums[mid]) {
return search(nums, low, mid - 1, target);
}
return search(nums, mid + 1, high, target);
}
}
}
q33_搜索旋转排序数组
作品《LeetCode题目分类与面试问题整理 - q33_搜索旋转排序数组》由 不喝星巴克 发布于 匠果,转载请注明出处及链接地址:
http://www.jiangguo.net/c/9r6/zy4.html