问题:27. Remove Element
问题描述:https://leetcode.com/problems/remove-element/description/
从序列中移除指定元素,返回新序列的长度。
Python:
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
while val in nums:
nums.remove(val)
return len(nums)
好像要比O(n)遍历还要快一点,应该是Python做过优化??