5/09/2020

[LeetCode] 27. Remove Element

Problem : https://leetcode.com/problems/remove-element/

Use 2 pointers approach.

Time Complexity :  O ( N )
Space Complexity : O ( 1 )

class Solution(object):
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        
        p1, p2 = 0, 0
        
        while p2 < len(nums):
            if nums[p2] != val:
                nums[p1] = nums[p2]
                p1 += 1
            
            p2 += 1
            
        return p1

No comments:

Post a Comment