思路,分割数组,缩小数组直至数组中只有一个元素。
https://github.com/Alexchent/interview/blob/master/in_array.php
$a = [1,2,3,4,5,8,9,11,12];
$x = 5;
function find($need, $arr) {
var_dump($arr);
$count = count($arr);
if ($count == 1) {
if ($arr[0] == $need) return true;
return false;
}
$tmp = intval($count/2) - 1;
if ($arr[$tmp] == $need) return true;
if ($arr[$tmp] > $need) {
return find($need, array_slice($arr, 0, $tmp+1));
} else {
return find($need, array_slice($arr, $tmp + 1));
}
}
var_dump(find($x, $a));
网友评论