mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
27. 移除元素Go版本暴力法
This commit is contained in:
@ -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
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user