添加0704二分查找PHP版本

This commit is contained in:
nolanzzz
2021-09-01 18:44:41 -04:00
parent a66e1801c3
commit 325a09cee7

View File

@ -478,6 +478,38 @@ int search(int* nums, int numsSize, int target){
}
```
**PHP:**
```php
// 左闭右闭区间
class Solution {
/**
* @param Integer[] $nums
* @param Integer $target
* @return Integer
*/
function search($nums, $target) {
if (count($nums) == 0) {
return -1;
}
$left = 0;
$right = count($nums) - 1;
while ($left <= $right) {
$mid = floor(($left + $right) / 2);
if ($nums[$mid] == $target) {
return $mid;
}
if ($nums[$mid] > $target) {
$right = $mid - 1;
}
else {
$left = $mid + 1;
}
}
return -1;
}
}
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)