Python in-place non-lambda solution in 39ms


  • 0
    M

    "Your runtime beats 100.00% of pythonsubmissions."

    class Solution(object):
        def removeElement(self, nums, val):
            """
            :type nums: List[int]
            :type val: int
            :rtype: int
            """
            end_index = self._advance_end_index(nums, len(nums) - 1, val)
            
            i = 0
            while i <= end_index:
                if nums[i] == val:
                    nums[i] = nums[end_index]
                    nums[end_index] = val
                    end_index = self._advance_end_index(nums, end_index, val)
                i += 1
            
            return end_index+1
        
        def _advance_end_index(self, nums, end_index, val):
            while end_index >= 0 and nums[end_index] == val:
                end_index -= 1
            return end_index```

Log in to reply
 

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