-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
92 lines (85 loc) · 2.34 KB
/
main.cpp
File metadata and controls
92 lines (85 loc) · 2.34 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <vector>
#include <map>
using namespace std;
class Solution
{
public:
/**
* Removes duplicates from input vector. Non-decreasing order is assumed. Performs in-place replacement with O(n) time complexity.
*
* @param nums Input number vector
* @return Overall sum of non-repeating elements (for 1,1,2,2,2,3 it will be 5)
*/
int removeDuplicates(vector<int>& nums)
{
int size = (int)nums.size();
if (size == 1)
return 1;
else if (size == 2)
return 2;
int current_num = nums[0];
int current_count = 1;
int overall_count = 0;
int k = 0;
for (int i = 1; i < size; ++i)
{
if (nums[i] != current_num)
{
if (current_count == 1)
{
nums[k] = current_num;
k++;
overall_count++;
}
else if (current_count >= 2)
{
nums[k] = nums[k + 1] = current_num;
k += 2;
overall_count += 2;
}
else
exit(1);
current_num = nums[i];
current_count = 0;
}
if (i + 1 == size)
{
current_count++;
if (current_count == 1)
{
nums[k] = current_num;
k++;
overall_count++;
}
else if (current_count >= 2)
{
nums[k] = nums[k + 1] = current_num;
k += 2;
overall_count += 2;
}
else
exit(1);
}
if (nums[i] == current_num)
current_count++;
}
return overall_count;
}
};
void print_n_vector_elements(vector<int>& array, int n)
{
for (int i = 0; i < n; ++i)
std::cout << array[i] << " ";
std::cout << std::endl;
}
int main()
{
vector<int> nums = { 0,0,1,1,1,1,2,3,3 };
print_n_vector_elements(nums, (int)nums.size());
Solution solution;
int k = solution.removeDuplicates(nums);
std::cout << "k: " << k << std::endl;
print_n_vector_elements(nums, k);
return 0;
}