class Solution {
/**
* @param Integer[] $nums
* @param Integer $target
* @return Integer
*/
function search ($nums, $target)
{
return $this->childSearch($nums, $target, 0, sizeof($nums) - 1);
}
function childSearch ($nums, $target, $low, $high)
{
if (empty($nums)) {
return -1;
}
// 数组剩一个数据
if ($low==$high){
if ($target==$nums[$low]){
return $low;
}else{
return -1;
}
}
if ($low > $high) {
return -1;
}
$mid = ceil(($low + $high) / 2);
if ($target == $nums[$mid]) {
return $mid;
}
// 如果中间值小于尾值,右侧为有序
if ($nums[$mid] <= $nums[$high]) {
if ($nums[$mid] < $target && $target <= $nums[$high]) {
return $this->childSearch($nums, $target, $mid + 1, $high);
} else {
return $this->childSearch($nums, $target, $low, $mid - 1);
}
} else {
// 左侧有序
if ($target < $nums[$mid] && $target >= $nums[$low]) {
return $this->childSearch($nums, $target, $low, $mid-1);
} else {
return $this->childSearch($nums, $target, $mid + 1, $high);
}
}
}
}