From 14aee4922bc43138b18f10ce52eb521706749b7b Mon Sep 17 00:00:00 2001 From: ArthurP Date: Tue, 31 Aug 2021 12:32:20 +0800 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.md=20C=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index e3b75719..886ce4f2 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -246,6 +246,23 @@ func removeElement(_ nums: inout [Int], _ val: Int) -> Int { } ``` +C: +```c +int removeElement(int* nums, int numsSize, int val){ + int slow = 0; + for(int fast = 0; fast < numsSize; fast++) { + //若快指针位置的元素不等于要删除的元素 + if(nums[fast] != val) { + //将其挪到慢指针指向的位置,慢指针+1 + nums[slow++] = nums[fast]; + } + } + //最后慢指针的大小就是新的数组的大小 + return slow; +} + +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321)