My c solution, easy to understand


  • 0
    I
    int cmp(const void *a, const void *b)
    {
        return *(int *)a - *(int *)b;
    }
    
    int removeElement(int *nums, int numsSize, int val)
    {
        if (NULL == nums || numsSize == 0) {
            return 0;
        }
    
        int i = 0;
        int begin = 0;
        int end = 0;
        int size = 0;
    
        qsort(nums, numsSize, sizeof(int), cmp);
    
        while (nums[i] != val && i < numsSize) {
            i++;
        }
        begin = i;
    
        while (nums[i] == val && i < numsSize) {
            i++;
        }
        end = i;
    
        size = numsSize - end + begin;
    
        for (i = end; i < numsSize; i++) {
            nums[begin++] = nums[i];
        }
    
        return size;
    }

Log in to reply
 

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