From b985828838d53dc28eaf11316a2be5e0469e92d9 Mon Sep 17 00:00:00 2001 From: KingArthur0205 Date: Wed, 23 Aug 2023 12:19:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A00283.=E6=BF=80=E5=8A=A8?= =?UTF-8?q?=E9=9B=B6=20C=E8=AF=AD=E8=A8=80=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0283.移动零.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0283.移动零.md b/problems/0283.移动零.md index 42232cc0..fc708844 100644 --- a/problems/0283.移动零.md +++ b/problems/0283.移动零.md @@ -151,6 +151,24 @@ function moveZeroes(nums: number[]): void { }; ``` +### C + +```c +void moveZeroes(int* nums, int numsSize){ + int fastIndex = 0, slowIndex = 0; + for (; fastIndex < numsSize; fastIndex++) { + if (nums[fastIndex] != 0) { + nums[slowIndex++] = nums[fastIndex]; + } + } + + // 将slowIndex之后的元素变为0 + for (; slowIndex < numsSize; slowIndex++) { + nums[slowIndex] = 0; + } +} +``` +