-
Notifications
You must be signed in to change notification settings - Fork 0
/
80. Remove Duplicates from Sorted Array II.java
69 lines (69 loc) · 1.78 KB
/
80. Remove Duplicates from Sorted Array II.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Solution {
public void leftShift(int []nums,int from,int to,int pos)
{
if(from<to)
{
while(pos<to && from<to)
{
nums[from]=nums[pos];
from++;
pos++;
}
}
}
public int removeDuplicates(int[] nums) {
if(nums.length==1)
{
return 1;
}
int count=0;
int start=0;
int len=nums.length;
int num=nums[0];
for(int i=nums.length-1;i>=0;i--)
{
if(nums[i]==nums[nums.length-1])
{
count++;
}
else
{
break;
}
}
if(count>2)
{
len-=(count-2);
}
count=0;
for(int i=0;i<len;)
{
if(nums[i]==num)
{
count++;
i++;
}
else
{
if(count>2)
{
leftShift(nums,start+2,len,i);
len-=(count-2);
i=start+2;
count=0;
start=i;
}
else
{
start=i;
count=1;
num=nums[i];
i++;
}
}
}
return len;
}
}