22 lines
480 B
Go
22 lines
480 B
Go
|
package main
|
||
|
|
||
|
func removeElement(nums []int, val int) int {
|
||
|
// 新陣列的邊界
|
||
|
slowPtr := 0
|
||
|
|
||
|
// 快指針,要獲取新陣列的元素
|
||
|
for fastPointer := 0; fastPointer < len(nums); fastPointer++ {
|
||
|
// 如果下一個元素不是我們要排除的
|
||
|
if nums[fastPointer] != val {
|
||
|
// 將元素加到我們的新的陣列中
|
||
|
nums[slowPtr], nums[fastPointer] = nums[fastPointer], nums[slowPtr]
|
||
|
// 擴容邊界
|
||
|
slowPtr++
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return slowPtr
|
||
|
}
|
||
|
|
||
|
func main() {}
|