-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathremoveDup2.java
38 lines (34 loc) · 897 Bytes
/
removeDup2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
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) {
// Start typing your Java solution below
// DO NOT write main() function
int size = A.length;
if (size <= 1)
return size;
int fast = 1;
int slow = 0;
int dup = 0;
while ( fast < size) {
if (A[slow] != A[fast]) {
A[slow+1] = A[fast];
slow++;
dup = 0;
} else{
dup++;
if (dup < 2) {
A[slow+1] = A[fast];
slow++;
}
}
fast++;
}
return slow+1;
}
}