-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRemoveDuplicates.py
More file actions
32 lines (28 loc) · 889 Bytes
/
RemoveDuplicates.py
File metadata and controls
32 lines (28 loc) · 889 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 080. Remove Duplicates from Sorted Array II
# 参见 # 026. Remove Duplicates from Sorted Array
class Solution:
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
if len(nums) == 1:
return 1
total = 1
num = 1
pointer = 1
while pointer < len(nums):
if nums[pointer-1] == nums[pointer]:
num += 1 # 计数当前遍历到的元素的个数
if num <= 2:
total += 1
pointer += 1
else:
nums.pop(pointer) # 加入计数大于2,将其pop
else: # 不同时,重置num
num = 1
total += 1
pointer += 1
return total