Skip to content

Commit 2572ddb

Browse files
add problem 26 and test function
1 parent 6d8c3a5 commit 2572ddb

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
3+
4+
* Do not allocate extra space for another array, you must do this in place with constant memory.
5+
6+
* For example,
7+
* Given input array nums = [1,1,2],
8+
9+
* 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.
10+
* Created by supercoderx on 2017/8/11.
11+
*/
12+
13+
#include <stdio.h>
14+
15+
int removeDuplicates(int *nums, int numsSize) {
16+
int len = numsSize;
17+
if (numsSize <= 1)
18+
return numsSize;
19+
int i = 0;
20+
while (i < len - 1) {
21+
if (nums[i] == nums[i + 1]) {
22+
for (int j = i + 1; j < len; j++) {
23+
nums[j - 1] = nums[j];
24+
}
25+
len--;
26+
i--;
27+
}
28+
i++;
29+
}
30+
31+
return len;
32+
}
33+
34+
void testRemoveDuplicates() {
35+
int a1[] = {1, 1, 1, 2, 2, 3, 4, 4};
36+
int count = removeDuplicates(a1, 7);
37+
for (int i = 0; i < count; i++)
38+
printf("%d,", a1[i]);
39+
}

0 commit comments

Comments
 (0)