-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveDuplicates.cpp
More file actions
60 lines (52 loc) · 1.39 KB
/
removeDuplicates.cpp
File metadata and controls
60 lines (52 loc) · 1.39 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
/*
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the new length.
*/
//my Solution
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int size;
do
{
size=nums.size();
auto it=unique(nums.begin(),nums.end());
nums.resize(distance(nums.begin(),it));
}while(size!=nums.size());
return nums.size();
}
};
//better Solution
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int size=nums.size();
if(size<2)
return size;
int id=1;
for(int i=1;i!=size;i++)
if(nums[i]!=nums[i-1])
nums[id++]=nums[i];
return id;
}
};
//better Solution
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int size=nums.size();
int cnt=0;
for(int i=1;i<size;i++)
{
if(nums[i]==nums[i-1])
cnt++;
else
nums[i-cnt]=nums[i];
}
return size-cnt;
}
};