My Python Solution, 42ms, O(n)

  • 0
    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):
        i = 0
        for j in range(len(A)):
            if A[j] != elem:
                A[i] = A[j]
                i += 1
        return i

  • 0

    It depends the test cases.
    I test your code, but the run time is 63ms.
    If the account of elem in A is large, your code is effective.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.