LintCode Q101 Remove Duplicates from Sorted Array II in Python

  • Jinhai ZHOU
  • 3 Minutes
  • 2016年12月13日
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
class Solution:
"""
@param A: a list of integers
@return an integer
"""
def removeDuplicates(self, A):
# write your code here
if len(A) <= 2:
return len(A)
current = 0
runner = 1
duplicate = False
while runner < len(A):
if A[runner] == A[current]:
if duplicate:
runner += 1
else:
A[current + 1] = A[runner]
runner += 1
current += 1
duplicate = True
else:
A[current + 1] = A[runner]
duplicate = False
current += 1
runner += 1
return current + 1
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。