Python 33ms Solution


  • 0
    W
    class Solution(object):
        def removeElement(self, nums, val):
            """
            :type nums: List[int]
            :type val: int
            :rtype: int
            """
            i, j = 0, len(nums) - 1
            
            while i < j:
                if nums[j] == val:
                    j -= 1
                    continue
                if nums[i] == val:
                    nums[i], nums[j] = nums[j], nums[i]
                i += 1
            if j == -1:
                return 0
            while nums[j] == val:
                j -= 1
                if j == -1:
                    return 0
            return j + 1
            
    

Log in to reply
 

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