Advertisement
DeepRest

Remove Duplicates from Sorted Array II

Feb 6th, 2022 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.51 KB | None | 0 0
  1. class Solution:
  2.     def removeDuplicates(self, nums: List[int]) -> int:
  3.         n = len(nums)
  4.         k = 0
  5.         for i in range(n):
  6. #no need of nums[i] != nums[k-1] only nums[i] != nums[k-2] will suffice and cover this case as array is sorted so if nums[i] is not equal to nums[k-1] then it has to be not equal to nums[k-2] as nums[k-2] <= nums[k-1]
  7.             if (k < 2) or (nums[i] != nums[k-1]) or (nums[i] != nums[k-2]):
  8.                 nums[k] = nums[i]
  9.                 k += 1
  10.         return k
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement