-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_80_RemoveDuplicatesFromSortedArrayII.cpp
59 lines (47 loc) · 1.36 KB
/
LC_80_RemoveDuplicatesFromSortedArrayII.cpp
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
/*
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
80. Remove Duplicates from Sorted Array II
https://binarysearch.com/problems/Remove-Duplicates-Occurring-More-Than-Twice
*/
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.size()==1) return 1;
// int i=0, j=1;
// bool flag = true;
// while(j<nums.size())
// {
// while(nums[i]==nums[j])
// {
// if(flag)
// {
// i++;
// swap(nums[i], nums[j]);
// flag = false;
// }
// j++;
// if(j>=nums.size()) return i+1;
// }
// i++;
// swap(nums[i], nums[j]);
// j++;
// flag=true;
// }
// return i+1;
int left=0, right=1;
int count = 1;
for( ; right<nums.size(); right++)
{
if(nums[left] == nums[right])
count +=1;
else
count = 1;
if(count<=2)
{
left++;
nums[left] = nums[right];
}
}
return left+1; // because zero indexing
}//end
};