From 325a09cee71a83261ba037f29eb27bb4f0b2522e Mon Sep 17 00:00:00 2001 From: nolanzzz Date: Wed, 1 Sep 2021 18:44:41 -0400 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A00704=E4=BA=8C=E5=88=86?= =?UTF-8?q?=E6=9F=A5=E6=89=BEPHP=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0704.二分查找.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0704.二分查找.md b/problems/0704.二分查找.md index 495f7367..f358d2be 100644 --- a/problems/0704.二分查找.md +++ b/problems/0704.二分查找.md @@ -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)