Share my two-pointers Solution


  • 0
    G
    class Solution(object):
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        length = len(nums)
        begin, end, count = 0, length-1, 0
        while begin <= end:
            if nums[begin] == val:
                nums[begin]= nums[end]
                del nums[end]
                count += 1
                end -= 1
            else:
                begin += 1
            
        return length-count

Log in to reply
 

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