Total Complexity | 10 |
Total Lines | 41 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
7 | class BinarySearch |
||
8 | { |
||
9 | public static function search(array &$nums, int $target): int |
||
10 | { |
||
11 | if (empty($nums)) { |
||
12 | return -1; |
||
13 | } |
||
14 | $n = count($nums); |
||
15 | [$left, $right] = [0, $n - 1]; |
||
16 | while ($left <= $right) { |
||
17 | $mid = $left + intdiv($right - $left, 2); |
||
18 | if ($nums[$mid] > $target) { |
||
19 | $right = $mid - 1; |
||
20 | } elseif ($nums[$mid] < $target) { |
||
21 | $left = $mid + 1; |
||
22 | } else { |
||
23 | return $mid; |
||
24 | } |
||
25 | } |
||
26 | |||
27 | return -1; |
||
28 | } |
||
29 | |||
30 | public static function search2(array &$nums, int $target): int |
||
48 | } |
||
49 | } |
||
50 |