From cb50b956ebc89019b915ce92174126f82de10e1a Mon Sep 17 00:00:00 2001 From: ch4r1ty <81227726+ch4r1ty@users.noreply.github.com> Date: Wed, 16 Oct 2024 00:01:58 -0400 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=860027.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=85=83=E7=B4=A0.md=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=BA=86java=E6=9A=B4=E5=8A=9B=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 480800e9..10817ba6 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -131,7 +131,24 @@ public: ## 其他语言版本 ### Java: - +```java +class Solution { + public int removeElement(int[] nums, int val) { + // 暴力法 + int n = nums.length; + for (int i = 0; i < n; i++) { + if (nums[i] == val) { + for (int j = i + 1; j < n; j++) { + nums[j - 1] = nums[j]; + } + i--; + n--; + } + } + return n; + } +} +``` ```java class Solution { public int removeElement(int[] nums, int val) {