3 line python solution; Easy to understand.


  • 0
    K
    def removeElement(self,nums,val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        while val in nums:
            nums.remove(val)
        return len(nums)
    

    A combination of remove() and while will make it quite easy to delete all the elements that are equal to val.


  • 1
    Q

    I am wondering if "in" and "remove" are too expensive...


  • 0
    K

    @qingpeng2 You are right bro. To be honest, this is the first problem I worked out and I did not realize how expensive these words are while writing lines. Now as I begin to have more exposure to a variety of problems here, I think I can get your point. Fortunately I did not get a "time limit exceeded" for this one lol.


Log in to reply
 

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