Two solutions both accepted best in C


  • 0
    //AC - 0ms;
    int removeElement(int* nums, int size, int val)
    {
        int l = 0, r = size-1;
        while(l <= r)
        {
            while(l <= r && nums[l] != val) l++;
            while(nums[r] == val) r--;
            if(l < r)
            {
                int t = nums[l];
                nums[l] = nums[r];
                nums[r] = t;
                r--;
            }
        }
        return l;
    }
    

    I tried to use the simplest solution to handle it, but much to my astonishment, it's accepted with the same performance as the previous comparably complex one. I do not understand what the hint here is used for, to mislead us to try weirder solution? Perhaps ##


    //AC - 0ms;
    int removeElement(int* nums, int size, int val)
    {
        int index = 0;
        for(int i = 0; i < size; i++)
            if(nums[i] != val)
                nums[index++] = nums[i];
        return index;
    }

Log in to reply
 

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