From 3441bced1289c183f9e05b005c1a67a4971014a2 Mon Sep 17 00:00:00 2001 From: nolanzzz Date: Wed, 1 Sep 2021 18:56:34 -0400 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200027.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E5=85=83=E7=B4=A0=20PHP=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index e3b75719..7976dce3 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -246,6 +246,31 @@ func removeElement(_ nums: inout [Int], _ val: Int) -> Int { } ``` +PHP: +```php +class Solution { + /** + * @param Integer[] $nums + * @param Integer $val + * @return Integer + */ + function removeElement(&$nums, $val) { + if (count($nums) == 0) { + return 0; + } + // 快慢指针 + $slow = 0; + for ($fast = 0; $fast < count($nums); $fast++) { + if ($nums[$fast] != $val) { + $nums[$slow] = $nums[$fast]; + $slow++; + } + } + return $slow; + } +} +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321)