O(n) Python


  • 0
    F
    Another way
    ``` 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[i] == val:
                    while nums[j] == val:
                        j -= 1
                        if i > j: break
                    else:
                        nums[i] = nums[j]
                        j -= 1
                i = i+1
            
            nums[:] = nums[:j+1]
            return len(nums)
    

Log in to reply
 

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