title | categories | ||
---|---|---|---|
移除元素 |
|
package main
/*
* @lc app=leetcode.cn id=27 lang=golang
*
* [27] 移除元素
*/
// @lc code=start
func removeElement(nums []int, val int) int {
slow, fast := 0, 0
for fast < len(nums) {
if val != nums[fast] {
nums[slow] = nums[fast]
slow++
}
fast++
}
return len(nums[:slow])
}
// @lc code=end