smj007

Untitled

Jul 30th, 2023
1,183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. """
  2. A1: Slow and fast pointer approach: The overall idea is to copy the non-target values from the array to the first few elements. For that, maintain a fast and slow pointer and iterate over slow pointer only if you encounter the non-target value. Once that is seen, what you do is overwrite the elements to the first part of the array
  3. """
  4.  
  5. class Solution:
  6.     def removeElement(self, nums: List[int], val: int) -> int:
  7.  
  8.         count = 0
  9.  
  10.         for i in range(len(nums)):
  11.             if nums[i]!=val:
  12.                 nums[count] = nums[i]
  13.                 count = count + 1
  14.  
  15.         return count
Advertisement
Add Comment
Please, Sign In to add comment