PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php class Solution { /** * @param Integer[] $nums * @param Integer $target * @return Integer */ static function search($nums, $target) { $head = 0; $tail = count($nums) - 1; while($head <= $tail) { $current = round(($head + $tail) / 2); if ($nums[$current] == $target) { return $current; } if ($nums[$current] < $target) { $head = $current + 1; } else { $tail = $current - 1; } } return -1; } } var_dump(Solution::search([-1, 0, 2], 2));
Show:  
Copy Clear