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.
1.两个指针 count index
2.当 A[index]!=elem时候就A[count]=A[index]
we need two pointers count and index
when A[index]!=elem A[count]=A[index]
class Solution:
# @param A a list of integers
# @param elem an integer, value need to be removed
# @return an integer
def removeElement(self, A, elem):
count=0
index=0
while index<len(A):
if A[index]!=elem:
A[count]=A[index]
count+=1
index+=1
return count
本文介绍了一种在数组中移除特定值并返回新长度的算法。通过使用两个指针,一个用于计数,另一个用于遍历数组,该算法可以在原地修改数组,忽略被移除元素之后的内容。
291

被折叠的 条评论
为什么被折叠?



