题目
原题页面:https://leetcode.com/problems/remove-element/
题目类型:Array, Two Pointers
难度评价:Easy
类似题目:(E) Remove Linked List Elements, (E) Move Zeroes
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
分析
题目已经暗示可以将需要删除的元素移至数组末尾,因此用两个下标m和n,m用来从左至右遍历数组寻找该元素,n从右至左记录可以用来交换的尾部位置。
代码
1 | class Solution: |
