From dbac875d31de722ff7c90bac778364956511b821 Mon Sep 17 00:00:00 2001 From: markwang Date: Tue, 19 Mar 2024 16:25:36 +0800 Subject: [PATCH] =?UTF-8?q?27.=20=E7=A7=BB=E9=99=A4=E5=85=83=E7=B4=A0Go?= =?UTF-8?q?=E7=89=88=E6=9C=AC=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 2a2005d7..967a7596 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -276,6 +276,24 @@ class Solution: ### Go: +```go +// 暴力法 +// 时间复杂度 O(n^2) +// 空间复杂度 O(1) +func removeElement(nums []int, val int) int { + size := len(nums) + for i := 0; i < size; i ++ { + if nums[i] == val { + for j := i + 1; j < size; j ++ { + nums[j - 1] = nums[j] + } + i -- + size -- + } + } + return size +} +``` ```go // 快慢指针法 // 时间复杂度 O(n) @@ -318,7 +336,6 @@ func removeElement(nums []int, val int) int { right-- } } - fmt.Println(nums) return left } ```