-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathas.php
More file actions
40 lines (37 loc) · 877 Bytes
/
Copy pathas.php
File metadata and controls
40 lines (37 loc) · 877 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
class Solution
{
public function search($nums, $target)
{
$l = 0;
$h = count($nums) - 1;
if ($target < $nums[$l]) {
return 0;
}
if ($target > $nums[$h]) {
return $h + 1;
}
while ($l <= $h) {
$mid = (int) (($l + $h) / 2);
if ($nums[$mid] == $target) {
while($mid >= 0 && $nums[$mid] == $target){
$mid-=1;
$flag=1;
}
$mid = ($flag==1) ? $mid+1 : $mid ;
return $mid;
} elseif ($nums[$mid] > $target) {
$h = $mid - 1;
} else {
$l = $mid + 1;
}
}
return $l;
}
}
$obj = new Solution();
$n = [1,2,2,4];
$t = 3;
echo "<pre>";
print_r($obj->search($n, $t));
echo "</pre>";