14th & 15 remove duplicates from sorted array I & II

Source: Internet
Author: User

Remove duplicates from sorted array I

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

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

Your function shocould return length =2, And a is now[1, 2].

Answer:

public class Solution {    public int removeDuplicates(int[] A) {        int length = A.length;        if(length==0 || length==1) return length;              for(int i=1; i<length;i++){            if(A[i-1]==A[i]){                for(int j=i; j<length-1; j++) A[j]=A[j+1];                //A[length-1]='\0';                            length--;                i--;            }        }        return length;            }}
Although the answer is correct, O (n) is the square of N, and time limit exceeded.

Solution:

public class Solution {    public int removeDuplicates(int[] A) {        int length = A.length;        if(length==0 || length==1) return length;        int next=1;        length=1;        for(int i=1; i<A.length;i++){            if(A[i-1]!=A[i]){                A[next] = A[i];                next++;                length++;            }        }        return length;    }}
This method only needs to traverse once and fill in the number that appears only once in a [next]. The time complexity is O (n ).


Remove duplicates from sorted array II

Follow up for "remove duplicates ":
What if duplicates are allowed at most twice?

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

Your function shocould return length =5, And a is now[1, 1, 2, 2, 3].

Solution1:

public class Solution {    public int removeDuplicates(int[] A) {        int length = A.length;        if(length<3) return length;        int times =0;         for(int i=1; i< length;i++){            if(A[i-1]==A[i]) times++;            else{ times=0;}            if(times>=2){                for(int j=i; j<length-1; j++){                    A[j] = A[j+1];                }                //A[length-1]= '\0';                times--;                i--;                length--;            }               }        return length;            }}
This method deletes an element that has been repeated for more than two times and moves the subsequent elements forward. Although it passes, the time complexity is n square, which is not a good method.

Solution2:

public class Solution {    public int removeDuplicates(int[] A) {        int length = A.length;        if(length<3) return length;        int times=0, next=1;        length=1;        for(int i=1; i< A.length;i++){            if(A[i-1]==A[i]) times++;            else{ times=0;}            if(times<2){                A[next]=A[i];                next++;                length++;            }        }        return length;    }}

This method only needs to traverse the array once, and the time complexity is O (n ). Record the number of times of an element, use next to record the moving subscript, and use length to record the moving length. If Times <2, the value of a [I] is moved to a [next]. Otherwise, nothing is done and only the subscript I is moved.


Note:

Looking at the answers from other students, some of them set the end of the newly generated array to '\ 0', as shown in the comments statement in the code. In Java, the array does not have an ending character. Even if the end of the array is set to '\ 0', the array does not end, but the element value is set to 0. The modified array is mainly determined by the elements in the array and the returned int value, that is, the new length of the array.

14th & 15 remove duplicates from sorted array I & II

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.