## [leetcode] python Remove Duplicates from Sorted Array II 原

ludlows

problem:

What if duplicates are allowed at most twice?

For example,
Given sorted array A = `[1,1,1,2,2,3]`,

Your function should return length = `5`, and A is now `[1,1,2,2,3]`.

solution：

``````class Solution:
# @param A a list of integers
# @return an integer
def removeDuplicates(self,A):
n = len(A)
if n ==0 :
return 0
index = 0
times = 0
for i in xrange(n):
if A[index] == A[i]:
times += 1
if times ==2 :
index += 1
A[index] = A[i]
else:
index += 1
A[index] = A[i]
times = 1
A = A[:index+1]
return len(A)``````

``````class Solution:
# @param A a list of integers
# @return an integer
def removeDuplicates(self,A):
n = len(A)
if n <= 2 :
return n
index = 2

for i in xrange(2,n):
if A[i] != A[index-2]:
A[index] = A[i]
index += 1

A = A[:index]
return len(A)``````

### ludlows

LeetCode目录。

Leafage_M
2017/11/21
0
0
Leetcode 80. Remove Duplicates from Sorted Array II

SnailTyan
2018/08/23
0
0
LeetCode Question Difficulty Distribution 问题难度和频率分布

Leetcode问题难度和频率分布表 引用自： https://zephyrusara.blogspot.jp/2014/07/leetcode-question-difficulty.html LeetCode Question Difficulty Distribution : Sheet1......

xidiancoder
2017/09/10
0
0

qq_32690999
2018/01/25
0
0
Leetcode 26. Remove Duplicates from Sorted Array

SnailTyan
2018/08/22
0
0

Httpd 整合 Tomcat 步骤

ZeroneLove

1
0
Docker笔记3——容器命令（未写完，明天整理接着写）

HappyBKs

1
0
2018个人年终总结

12
0

linux-tao

3
0
Spark共享变量

2
0