mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
添加0704二分查找PHP版本
This commit is contained in:
@ -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)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
|
Reference in New Issue
Block a user