0027.移除元素
方法一:双指针
时间复杂度 ,空间复杂度 。
func removeElement(nums []int, val int) int {
first, last := 0, 0
for ; last < len(nums); last++ {
if nums[last] != val {
nums[first], first = nums[last], first+1
}
}
return first
}
时间复杂度 ,空间复杂度 。
func removeElement(nums []int, val int) int {
first, last := 0, 0
for ; last < len(nums); last++ {
if nums[last] != val {
nums[first], first = nums[last], first+1
}
}
return first
}