Description
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
My Solution
Python
1 |
class (object): |
- 想法:分别从两端进行遍历,将head==val的值交换到tail处。head和tail相当于两个指针
Best Solution
Python (Simpler my solution)
1 |
def removeElement(self, nums, val): |
- 想法:O(n),本质上和我的方法一致
近期评论