A Python solution


  • 0
    S
    class Solution(object):
        def removeElement(self, nums, val):
            """
            :type nums: List[int]
            :type val: int
            :rtype: int
            """
            i = 0
            while i < len(nums):
                if nums[i] == val:
                    tmp = nums.pop()
                    if i == len(nums):
                        return i
                    nums[i] = tmp
                else:
                    i += 1
            return i
    

Log in to reply
 

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