Skip to content

Instantly share code, notes, and snippets.

@Ray1988
Created June 11, 2014 05:16
Show Gist options
  • Save Ray1988/f698b30a7eebebe00fda to your computer and use it in GitHub Desktop.
Save Ray1988/f698b30a7eebebe00fda to your computer and use it in GitHub Desktop.
python
"""
Follow up for "Remove Duplicates":
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].
"""
class Solution:
# @param A a list of integers
# @return an integer
def removeDuplicates(self, A):
if len(A)<3:
return len(A)
temp=A[1]
length=1
for i in range (2, len(A)):
if A[i]!=A[i-2]:
A[length]=temp
length+=1
temp=A[i]
A[length]=temp
length+=1
return length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment