Conditions | 5 |
Paths | 5 |
Total Lines | 19 |
Code Lines | 12 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
1 | <?php |
||
24 | public static function fixedPoint2(array $nums): int |
||
25 | { |
||
26 | if (empty($nums)) { |
||
27 | return 0; |
||
28 | } |
||
29 | |||
30 | [$low, $high] = [0, $nums[count($nums) - 1]]; |
||
31 | while ($low <= $high) { |
||
32 | $mid = (int) ($low + ($high - $low) / 2); |
||
33 | if ($nums[$mid] === $mid) { |
||
34 | return $mid; |
||
35 | } elseif ($mid > $nums[$mid]) { |
||
36 | $low++; |
||
37 | } else { |
||
38 | $high--; |
||
39 | } |
||
40 | } |
||
41 | |||
42 | return -1; |
||
43 | } |
||
45 |