Forked from Ray1988/RemoveDuplicatesfromSortedArrayII.py
Created
July 7, 2016 23:10
-
-
Save dafma/36618feec7a97586ae66421222fe7255 to your computer and use it in GitHub Desktop.
python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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