aboutsummaryrefslogtreecommitdiff
path: root/store/works/solutions/leetcode/cpp/26.cpp
blob: 3ee4aa740e20c4d7d2f21bf8f3ae2b80962e265e (plain)
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
#include <vector>

using std::vector;

class Solution
{
public:
    int removeDuplicates(vector<int> &nums)
    {
        if (nums.empty())
            return 0;

        auto iter_head = nums.cbegin();
        auto iter = iter_head + 1;
        int current = nums.front();
        int count = 1;

        while (iter != nums.cend())
        {
            const auto v = *iter;
            if (v == current)
            {
                nums.erase(iter);
                iter = iter_head + 1;
            }
            else
            {
                current = v;
                count++;
                iter_head = iter;
                ++iter;
            }
        }

        return count;
    }
};