Skip to content

Commit a5db993

Browse files
committed
添加(0035.搜索插入位置.md):增加PHP版本
1 parent bda7b41 commit a5db993

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

problems/0035.搜索插入位置.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,31 @@ func searchInsert(_ nums: [Int], _ target: Int) -> Int {
318318
```
319319

320320

321+
### PHP
322+
323+
```php
324+
// 二分法(1):[左闭右闭]
325+
function searchInsert($nums, $target)
326+
{
327+
$n = count($nums);
328+
$l = 0;
329+
$r = $n - 1;
330+
while ($l <= $r) {
331+
$mid = floor(($l + $r) / 2);
332+
if ($nums[$mid] > $target) {
333+
// 下次搜索在左区间:[$l,$mid-1]
334+
$r = $mid - 1;
335+
} else if ($nums[$mid] < $target) {
336+
// 下次搜索在右区间:[$mid+1,$r]
337+
$l = $mid + 1;
338+
} else {
339+
// 命中返回
340+
return $mid;
341+
}
342+
}
343+
return $r + 1;
344+
}
345+
```
321346

322347

323348
-----------------------

0 commit comments

Comments
 (0)