From 69d78bd5348f858e68a3ee8e88ccf006e421565f Mon Sep 17 00:00:00 2001 From: wangsiyuan <2392948297@qq.com> Date: Tue, 12 Sep 2023 16:43:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20Solution.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/solution/Solution.java | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/solution/Solution.java b/src/solution/Solution.java index e0df66c..68a4b00 100644 --- a/src/solution/Solution.java +++ b/src/solution/Solution.java @@ -174,15 +174,10 @@ public class Solution { * 元素的 相对顺序 应该保持 一致 。然后返回 nums 中唯一元素的个数。 */ public static int removeDuplicates(int[] nums) { - int i = 0; - for (int j = 0; j < nums.length; j++) { - if (j == 0){ - nums[i] = nums[j]; - i++; - continue; - } - int preValue = nums[j - 1]; - if (nums[j] != preValue){ + if (nums.length == 0) return 0; + int i = 1; // 从第二个元素开始检查 + for (int j = 1; j < nums.length; j++) { + if (nums[j] != nums[j - 1]) { nums[i] = nums[j]; i++; }