LeetCode

27 移除元素

package main

//https://leetcode-cn.com/problems/remove-element/
func main() {

}

func removeElement(nums []int, val int) int {
    for i := 0; i < len(nums); {
        if nums[i] == val {
            nums = append(nums[:i], nums[i+1:]...)
        } else {
            i++
        }

    }
    return len(nums)
}

func removeElement2(nums []int, val int) int {
    left := 0
    for _, v := range nums {
        if v != val {
            nums[left] = v
            left++
        }

    }
    return left
}

回复

This is just a placeholder img.