[Leetcode] Remove Duplicates from Sorted Array

Source: Internet
Author: User

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

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 [all].

This problem is required to delete a repeating element in an ordered array.

Because it is an ordered array, the elements of the same value must be in a contiguous position, with the idea of inserting a sort, initially seeing the first element as a non-repeating ordered table, and then sequentially judging that the subsequent element is not the same as the last element of the previous non-repeating ordered array. If the same, then continue to judge backwards, if different, then insert into the previous non-repeating ordered array last until the end of the judgment array.

class Solution {public:    intremoveDuplicates(intint n) {        if(!n)            return0;        int i=0,j=1;        for(;j<n;j++){            if(A[i]<A[j]){                A[++i]=A[j];            }        }        return i+1;    }};

[Leetcode] Remove Duplicates from Sorted Array

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.