From 79fd10a6c498b5a3313738da41e653ccb1ab4580 Mon Sep 17 00:00:00 2001 From: posper Date: Wed, 28 Jul 2021 14:13:51 +0800 Subject: [PATCH] =?UTF-8?q?283.=20=E7=A7=BB=E5=8A=A8=E9=9B=B6=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0283.移动零.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0283.移动零.md b/problems/0283.移动零.md index 56f96b2f..8db42a0a 100644 --- a/problems/0283.移动零.md +++ b/problems/0283.移动零.md @@ -64,6 +64,21 @@ public: Java: +```java +public void moveZeroes(int[] nums) { + int slow = 0; + for (int fast = 0; fast < nums.length; fast++) { + if (nums[fast] != 0) { + nums[slow++] = nums[fast]; + } + } + // 后面的元素全变成 0 + for (int j = slow; j < nums.length; j++) { + nums[j] = 0; + } + } +``` + Python: ```python