Could anyone tell me what is worong with that C coding?


  • 0
    C
    int removeDuplicates(int* nums, int numsSize) {
        int i = 0;
        if(numsSize > 1)
        {
            
            for(int j = 0; j <= numsSize - 1; j++)
            {
                if(nums[i] != nums[j])
                {
                     nums[i] = nums[j];
                     i++;
                 }
             }
             return i;
        }
        else if (numsSize = 0)
        {
            return 0;
        }
        else if (numsSize = 1)
        {
            return 1;
        }
    }

  • -1
    W

    should skip the first element
    else if (numsSize = 0) and else if (numsSize = 1) should use ==

    corret answer should be like this

    int removeDuplicates(int* nums, int numsSize) {
        if (numsSize == 0)
            return 0;
        int i = 1;
        if(numsSize > 1)
        {
     
            for(int j = 1; j <= numsSize - 1; j++)
            {
                if(nums[i - 1] != nums[j])
                {
                     nums[i] = nums[j];
                     i++;
                 }
             }
        }
        return i;
    }
    
    

  • 0

    @wangzhw New users please read the instructions to format your code properly, please. Thank you!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.