Advertisement
smj007

Untitled

Mar 9th, 2025
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. class Solution:
  2.     def removeElement(self, nums: List[int], val: int) -> int:
  3.         """
  4.        Understanding the Logic:
  5.        We use two pointers:
  6.  
  7.        i → Iterates through all elements in the array.
  8.        k → Tracks the position where the next valid element should be placed.
  9.  
  10.        k represents the new length of the array after removal. Tracks the
  11.        position where the next valid element should be written.
  12.        So effectively, is just the slow pointer of tracker of all non-target
  13.        elements
  14.        """
  15.         k = 0
  16.         for i in range(len(nums)):
  17.             if nums[i] != val:
  18.                 nums[k] = nums[i]
  19.                 k += 1
  20.        
  21.         return k
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement