Remove Duplicates from Sorted Array II
1. Problem Statement
2. Algorithm Design and Approach
3. Implementation
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
class Solution
{
public:
int removeDuplicates(vector<int> &nums)
{
int cur = nums[0];
int flag = 0;
int len = nums.size();
for (auto it = nums.begin(); it != nums.end(); it++)
{
if (*it == cur)
{
if (flag > 1)
{
nums.erase(it);
it--;
len--;
}
else
{
flag++;
}
}
else
{
cur = *it;
flag = 1;
}
}
return len;
}
};
4. Example Walkthrough
5. Conclusion
This post is licensed under CC BY 4.0 by the author.