您的位置:首页 > 编程语言 > Python开发

27. Remove Element Leetcode Python

2015-01-29 11:53 405 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode python Array