leetcode_8. delete duplicate entries sorted array

  • Delete duplicate entries sorted array : Given a sorted array, you need to remove the recurring elements in place, so that each element appears only once, after returning the new length of the array is removed. Do not use extra space for an array, you must modify the input array in place and completed under the conditions of use O (1) extra space.
示例 1:

给定数组 nums = [1,1,2], 
函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。 

示例 2:

给定 nums = [0,0,1,1,1,2,2,3,3,4],
函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。

注意:你不需要考虑数组中超出新长度后面的元素。
int removeDuplicates(int* nums, int numsSize){
    if(numsSize==0)
    {
        return 0;
    }
    int i,j=1;
    for(i=1;i<numsSize;i++)
    {
        if(nums[i]!=nums[i-1])
        {
            nums[j]=nums[i];
            j++;
        }
    }
    return j;
}
Published 77 original articles · won praise 19 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_42932834/article/details/95373299