题目:一个升序排列的数组nums ,删除重复出现的元素,使每个元素只出现一次。
#include <stdio.h>
#include <stdlib.h>
int removeDuplicates(int* nums, int numsSize)
{
int i = 0;
int j;
for( j = 1; j < numsSize; j++ )
{
if(nums[j-1] < nums[j])
{
nums[++i] = nums[j];
}
}
return i+1;
}
int main()
{
int nums[] = {0,0,1,1,2,3,3,4,4,5};
int ret = removeDuplicates(nums,sizeof(nums)/sizeof(int));
for( int i = 0 ; i < ret; i++ )
{
printf("%d\t",nums[i]);
}
printf("\n");
return 0;
}
0 1 2 3 4 5