Leetcode: Remove Element

Source: Internet
Author: User

Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Solution 1

The solution I first came up with was to traverse from the past to the next and replace the elements that appear in sequence with elements that are not at the end of the array, because the meaning clearly indicates that the order of elements can be changed.

The specific implementation method is: set two iterators, one for forward scanning, one for backward scanning, the other for forward scanning to find the next target, and the other for scanning to find the last non-target element, exchange between the two (in fact, there is no need to exchange, you only need to assign the last element to the forward target address, because the meaning of the question clearly indicates that the element beyond the new length is not required ). This loops until the two iterators meet each other.

The Code is as follows:

C++ codeclass Solution {public:    int removeElement(int A[], int n, int elem) {        int forwardScan = 0, backwardScan = n - 1;        while (forwardScan <= backwardScan)        {            if      (A[forwardScan  ] != elem) forwardScan ++;            else if (A[backwardScan ] == elem) backwardScan--;            else     A[forwardScan++] =      A[backwardScan--];        }        return backwardScan + 1;    }};
Solution 2

InDiscussThere is a solution similar to the above, but there are two differences in implementation:

  • There is no need to use a backward iterator and the array length is N;
  • You do not need to find the last non-target element from the end and directly replace it with the forward target element. The next scan will continue from this element.

The specific implementation method is as follows:

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

In discuss, we also see another solution: instead of using the backward iterator, we can directly move non-target elements forward and overwrite all target elements.

This method has the following advantages:

  • Disadvantage: a large number of elements are moved. If the first element is the target, the entire array needs to be re-assigned;
  • Advantage: the sequence of the original array is ensured.

The specific implementation method is as follows:

C++ codeint removeElement(int A[], int n, int elem) {    int begin=0;    for(int i=0;i<n;i++)        if(A[i]!=elem) A[begin++]=A[i];    return begin;}

Leetcode: Remove Element

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.