Last active
March 28, 2019 12:50
-
-
Save BrambleXu/9edb6b2f23e4d1691ff041faf690d773 to your computer and use it in GitHub Desktop.
This file contains hidden or 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: | |
if len(nums) == 0: return 0 | |
if len(nums) == 1: return 1 | |
# nums = [0,0,1,1,1,2,2,3,3,4] | |
j = 1 # slover pointer, only move when meet unique number | |
for i in range(1, len(nums)): # faster pointer, i will iterate over all element in nums | |
if nums[i] != nums[i-1]: # when nums[i] is a unique number, assign it to nums[j] | |
nums[j] = nums[i] | |
j += 1 | |
# After for loop, i = 9, j = 5, nums = [0, 1, 2, 3, 4, 2, 2, 3, 3, 4] | |
# We have to delete duplicates backwards | |
for delete_index in range(i, j-1, -1): | |
del nums[delete_index] | |
return j | |
# ✔ 161/161 cases passed (60 ms) | |
# ✔ Your runtime beats 79.28 % of python3 submissions | |
# ✔ Your memory usage beats 5.43 % of python3 submissions (14.9 MB) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment