卖萌的弱渣

I am stupid, I am hungry.

Remove Element

Remove Element

Given an array and a value, remove all occurrences of that value in place and return the new length.

The order of elements can be changed, and the elements after the new length don’t matter.

Example

Given an array [0,4,4,0,0,2,4,4], value=4

return 4 and front four elements of the array is [0,0,0,2]

Note

  1. Return the length of new array A
  2. Don’t create another container

Solution

(Remove-Element.py) download
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
    """
    @param A: A list of integers
    @param elem: An integer
    @return: The new length after remove
    """
    def removeElement(self, A, elem):
        result = len(A)
        if A == None or len(A) == 0:
            return A
        while elem in A:
            A.remove(elem)
        return len(A)