Skip to content

Commit

Permalink
添加(0035.搜索插入位置.md):增加PHP版本
Browse files Browse the repository at this point in the history
  • Loading branch information
fmtvar committed May 15, 2022
1 parent bda7b41 commit a5db993
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions problems/0035.搜索插入位置.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,31 @@ func searchInsert(_ nums: [Int], _ target: Int) -> Int {
```


### PHP

```php
// 二分法(1):[左闭右闭]
function searchInsert($nums, $target)
{
$n = count($nums);
$l = 0;
$r = $n - 1;
while ($l <= $r) {
$mid = floor(($l + $r) / 2);
if ($nums[$mid] > $target) {
// 下次搜索在左区间:[$l,$mid-1]
$r = $mid - 1;
} else if ($nums[$mid] < $target) {
// 下次搜索在右区间:[$mid+1,$r]
$l = $mid + 1;
} else {
// 命中返回
return $mid;
}
}
return $r + 1;
}
```


-----------------------
Expand Down

0 comments on commit a5db993

Please sign in to comment.