题目难度:简单


// 我的错误答案
int removeDuplicates(int* nums, int numsSize){
    int i,k,l;
    int len = numsSize;
    for(i=0;i<numsSize;i++){
        for(k=i;k<numsSize;k++){
            if(nums[i]==nums[k]){
                for(l=k;l<numsSize-1;l++){
                    nums[l]=nums[l+1];
                }
                len--;
            }
        }
    }
    return len;
}


// 官方解答
int removeDuplicates(int* nums, int numsSize) {
    if (numsSize == 0) {
        return 0;
    }
    int fast = 1, slow = 1;
    while (fast < numsSize) {
        if (nums[fast] != nums[fast - 1]) {
            nums[slow] = nums[fast];
            ++slow;
        }
        ++fast;
    }
    return slow;
}

官方题解:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/solution/shan-chu-pai-xu-shu-zu-zhong-de-zhong-fu-tudo/

第一天,呜呜呜T.T

posted on 2022-03-14 22:46  北冥有鱼其名为嘉  阅读(22)  评论(0编辑  收藏  举报