mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-13 03:27:07 +00:00
Time: 56 ms (27.68%), Space: 16.5 MB (58.43%) - LeetHub
This commit is contained in:
+23
@@ -0,0 +1,23 @@
|
|||||||
|
class Solution:
|
||||||
|
def removeDuplicates(self, nums: List[int]) -> int:
|
||||||
|
readIndex = 1
|
||||||
|
writeIndex = 1
|
||||||
|
|
||||||
|
lastValueSeen = nums[0]
|
||||||
|
numTimesValueSeen = 1
|
||||||
|
while readIndex < len(nums):
|
||||||
|
if nums[readIndex] == lastValueSeen:
|
||||||
|
numTimesValueSeen += 1
|
||||||
|
|
||||||
|
if numTimesValueSeen <= 2:
|
||||||
|
nums[writeIndex] = nums[readIndex]
|
||||||
|
writeIndex += 1
|
||||||
|
else:
|
||||||
|
lastValueSeen = nums[readIndex]
|
||||||
|
numTimesValueSeen = 1
|
||||||
|
nums[writeIndex] = nums[readIndex]
|
||||||
|
writeIndex += 1
|
||||||
|
|
||||||
|
readIndex += 1
|
||||||
|
|
||||||
|
return writeIndex
|
||||||
Reference in New Issue
Block a user