imajinyun /
leetcode-php
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace leetcode; |
||
| 6 | |||
| 7 | class SearchInsertPosition |
||
| 8 | { |
||
| 9 | public static function searchInsert(array $nums, int $target): int |
||
| 10 | { |
||
| 11 | if (empty($nums)) { |
||
| 12 | return 0; |
||
| 13 | } |
||
| 14 | [$low, $high] = [0, count($nums) - 1]; |
||
| 15 | while ($low <= $high) { |
||
| 16 | $mid = (int) (($high + $low) / 2); |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 17 | $mid = $low + intdiv($high - $low, 2); |
||
| 18 | if ($nums[$mid] > $target) { |
||
| 19 | $high = $mid - 1; |
||
| 20 | } elseif ($nums[$mid] < $target) { |
||
| 21 | $low = $mid + 1; |
||
| 22 | } else { |
||
| 23 | return $mid; |
||
| 24 | } |
||
| 25 | } |
||
| 26 | |||
| 27 | return $low; |
||
| 28 | } |
||
| 29 | } |
||
| 30 |