LEETCODE26/27/80 Remove duplicates from Sorted Array I and Ii/remove element**

Source: Internet
Author: User

One: Remove duplicates from Sorted Array

Topic:

Given a sorted array, remove the duplicates in place such, all element appear only once and return the new L Ength.

Do the allocate extra space for another array, and you must does this on place with constant memory.

for example,
given input array A = [1,1,2] ,

Your function should return length = 2 , and A is now [1,2] .

Links: https://leetcode.com/problems/remove-duplicates-from-sorted-array/

Analysis: This problem is very simple, is to remove the ordered array of duplicate elements, here I give three ways.

French One: Violence

for (int i = 0; i < n; i++) {            int j = 1;            while ((i+j) <n && a[i] = = A[i+j]) {                j + +;            }            if (J > 1) {for                (int k = i+j; k < n; k++)                    a[k-j+1] = a[k];                n = n-j+1;                            }        }        return n
The result is tle, because when the element is [ -998,-998,-997,-997,-997 ...] each traversal, it is almost necessary to move o (n) times, obviously timeout

Law two: Set up a vector, much simpler, but need extra space, do not know how to pass on the Leetcode, the time complexity of O (n), but the space complexity is O (n) does not meet the requirements

if (n = = 0) return 0;        Vector <int>v;        V.push_back (A[0]);        for (int i = 1; i < n; i++) {            if (a[i]! = V.back ()) V.push_back (A[i]);        }        for (int i = 0; i < v.size (); i++)            a[i] = v[i];        return V.size ();

Fahsarmset up a calculator to count the number of distinct elements, when the elements are different, each time the elements are not the same, the counter plus 1, and the currently traversed elements into the counter corresponding to the position in the array. It is amazing!!! It can also be said that the idea of double pointers!!

Class Solution {Public:int removeduplicates (int a[], int n) {//Fayi:/* for (int i = 0; i < n; i++) {            int j = 1;            while ((i+j) <n && a[i] = = A[i+j]) {j + +;                } if (J > 1) {for (int k = i+j; k < n; k++) a[k-j+1] = a[k];                            n = n-j+1;        }} return n;*///Fah so can AC?        /*if (n = = 0) return 0;        Vector <int>v;        V.push_back (A[0]);        for (int i = 1; i < n; i++) {if (A[i]! = V.back ()) V.push_back (A[i]);        } for (int i = 0; i < v.size (); i++) a[i] = V[i];        return v.size (); *//Fahsarm It is amazing!!        if (n = = 0) return 0;        int index = 1;            for (int i = 1;i < n; i++) {if (A[i]! = A[i-1]) {a[index++] = A[i];            }} return index; }};

Two: Remove Element

Topic:

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 are you leave beyond the new length.

Links: https://leetcode.com/problems/remove-element/

Analysis: is to move the divisor group = = Specifies the Elem element.

Method One: First sort, after finding the element, after moving to delete,,--———— trouble complex ———— O (NLGN)

Sort (A, a+n);       Sort        int count = 0;        int index = 0;        for (int i = 0; i < n; i++) {    //record ==elem the number of elements and the last subscript            if (a[i] = = elem) {                 count + +;                index = i;            }        }        for (int i = index+1; i < n; i++) {     //move array            a[i-count] = A[i];        }        return n-count;

Method Two:The counter method, traversed once, if the element is not equal to Elem, puts the element in the position where the counter is positioned. The position of the counter is less than or equal to the subscript of the current traverse, so no subsequent traversal is affected. The time complexity is O (N).

Class Solution {public:    int removeelement (int a[], int n, int elem) {        int index = 0;        for (int i = 0; i < n; i++) {            if (a[i]! = elem)                a[index++] = A[i];        }        return index;            };

Three: Remove duplicates from Sorted Arrayii

Topic:

Follow up for "Remove duplicates":
What if duplicates is allowed at the most twice?

for example,
given sorted array A = [1,1,1,2,2,3] ,

Your function should return length = 5 , and A is now [1,1,2,2,3] .

Links: https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/

Analysis: Still the idea of counter + double pointers

Class Solution {public:    int removeduplicates (int a[], int n) {        if (n = = 0 | | n = = 1) return n;        int index = 1;        for (int i = 1; i < n-1; i++) {            if (a[i]! = A[i-1] | | A[i]! = a[i+1]) {   //By judging the previous element with the latter element                a[index++]= a[i];}        }        a[index++] = a[n-1];        return index;            };






LEETCODE26/27/80 Remove duplicates from Sorted Array I and Ii/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.