My 19 lines code in C. 0 ms


  • 0
    X
    int removeElement(int* nums, int numsSize, int val) {
        int left = 0;
        int right = numsSize - 1;
        if (nums == NULL) return 0;
        if (numsSize == 0) return 0;
        while(left < right) {
            while(right > 0 &&nums[right] == val) {
                right--;
            }
            if (left >= right) break;
            if (nums[left] == val) {
                nums[left] = nums[right];
                right--;
            }
            left++;
        }
        if (left == right && nums[left] != val) left++;
        return left;
    }

  • 1
    9
    int removeElement(int A[], int n, int elem) {
        int i = 0;
        while(i < n)
        {
            if(A[i] == elem) 
            {
                n --;
                A[i] = A[n];
            }
            else
                i++;
        }
        return n;
    }

  • 0
    S
    This post is deleted!

Log in to reply
 

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