Last active
November 1, 2022 00:59
-
-
Save superzjn/eb316ba9dddc37e0f141a963d76049f6 to your computer and use it in GitHub Desktop.
[Remove Duplicates from Sorted Array] LeetCode 26 #TwoPointers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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