12 ms O(N) C version, tag duplicate element


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

Log in to reply
 

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