27. 移除元素Go版本暴力法

This commit is contained in:
markwang
2024-03-19 16:25:36 +08:00
parent 253bddcf37
commit dbac875d31

View File

@ -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
}
```