博客域名:
http://www.xnerv.wang
原题页面: https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
题目类型:
难度评价:★
For example,
Given sorted array nums = [1,1,1,2,2,3],
原题页面: https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
题目类型:
难度评价:★
本文地址:http://blog.youkuaiyun.com/nerv3x3/article/details/38929163
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?For example,
Given sorted array nums = [1,1,1,2,2,3],
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.
class Solution:
# @param a list of integers
# @return an integer
def removeDuplicates(self, A):
if None == A:
return 0
len_A = len(A)
if len_A <= 1:
return len_A
m = 0
n = 1
count = 1
while n < len_A:
if A[m] != A[n]:
count = 1
m += 1
if m != n:
A[m] = A[n]
elif count >= 2:
count += 1
else:
m += 1
count += 1
if m != n:
A[m] = A[n]
n += 1
A = A[0:m+1] # A must be modified
return m + 1