Created
June 11, 2014 05:18
Java
This file contains 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]. | |
*/ | |
public class Solution { | |
public int removeDuplicates(int[] A) { | |
if (A==null){ | |
return 0; | |
} | |
if (A.length<3){ | |
return A.length; | |
} | |
int temp=A[1]; | |
int len=1; | |
for(int i=2; i<A.length; i++){ | |
if (A[i]!=A[i-2]){ | |
A[len++]=temp; | |
temp=A[i]; | |
} | |
} | |
A[len++]=temp; | |
return len; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment