Skip to content

Instantly share code, notes, and snippets.

@superzjn
Last active November 1, 2022 00:59
Show Gist options
  • Save superzjn/eb316ba9dddc37e0f141a963d76049f6 to your computer and use it in GitHub Desktop.
Save superzjn/eb316ba9dddc37e0f141a963d76049f6 to your computer and use it in GitHub Desktop.
[Remove Duplicates from Sorted Array] LeetCode 26 #TwoPointers
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
insert_index = 1
# Two pointer to the same direction
# We can also use range(1, len(nums)) to have better performance by skip comparing nums[0] and nums[0]
for i in range(len(nums)):
# find different numbers
if nums[i] != nums[insert_index - 1]:
nums[insert_index] = nums[i]
insert_index += 1
return insert_index
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment