leetcode简单题

Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.

题目条件: a sorted!!!一开始没注意,绕了很多弯子

题目要求:就地算法

代码如下:

int removeDuplicates(int* nums, int numsSize) {
    /*int i=0,no;
    int sum=1;
    no=nums[0];
    while(i<numSize){
        if(nums[i]!=no){
            sum++;
            i++;
            no=nums[i];
        }
        else{
            i++;
        }
    }
    print("%d",sum);*/
    int i,j;
    j=0;
    if(numsSize==0)
        return 0;
    else{
            for(i=1;i<numsSize;i++){
        if(nums[i]!=nums[j]){
            j++;
            nums[j]=nums[i];
        }
    }
    return j+1;
    }

}



总结:是不是有一点partion算法?

猜你喜欢

转载自blog.csdn.net/zimengxueying/article/details/80171879