-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_duplicated_in_sorted.cpp
More file actions
55 lines (39 loc) · 1.04 KB
/
remove_duplicated_in_sorted.cpp
File metadata and controls
55 lines (39 loc) · 1.04 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
/*
leetcode题目,将一个排序数组中的重复元素去除
2.1.1 Remove Duplicates from Sorted Array
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 A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
*
*/
#include <list>
#include <iostream>
using namespace std;
int remove_duplicate(int *in, int size)
{
int i = 0, j = 1;
int current_value;
if (!in)
return 0;
current_value = in[0];
for (i = 1; i < size; i++) {
if (in[i] != current_value) {
in[j++] = in[i];
current_value = in[i];
}
}
return j;
}
int main()
{
int in[] = {1, 5, 9, 11, 11, 11, 11, 15, 19};
int i;
int ret_size;
ret_size = remove_duplicate(in, 9);
for (i = 0; i < ret_size; i++) {
printf("%d, ", in[i]);
}
printf("\n");
return 0L;
}