Leetcode26: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 nums = [1,1,2],

Your function should return length = 2, with the first of the elements of Nums being 1 and 2 respectively. It doesn ' t matter what are you leave beyond the new length.

Solution One

STL has the function of removing duplicate elements unique, it is to remove the duplicate elements, but does not change the position of the element, and does not change the container's properties, such as the length of the container, the following is its description:
Removes all and the first element from every consecutive group of equivalent elements in the range [First,last].

The function cannot alter the properties of the object containing the range of elements (i.e., it cannot alter the size of An array or a container): The removal are done by replacing the duplicate elements by the next element, which is not a dupli Cate, and signaling the new size of the shortened range by returning a iterator to the element so should be considered Its new past-the-end element.

The relative order of the elements not removed was preserved while the elements between the returned iterator Left in a valid but unspecified state.

The distance function can then be used to return the number of elements in the container:
Runtime:32ms

class Solution {public:    int removeDuplicates(vector<int>& nums) {        return distance(nums.begin(),unique(nums.begin(),nums.end()));    }};
Solution Two

If you cannot use the unique function in STL, then you can use the idea of a unique function, define two pointers, one pointer to the current element, another pointer to an element that is different from the current element, and a value for the second pointer to the first pointer to the right.
Runtime:32ms

classSolution { Public:intRemoveDuplicates ( vector<int>& Nums) {intLength=nums.size ();if(length==0|| length==1)returnLengthintfirst=0;intlast=0; while(++last<length) {if(!            (Nums[first]==nums[last]))            {Nums[++first]=nums[last]; }        }returnfirst+1; }};

Attached: The following is the implementation code for the unique function in the STL:

Template <class forwarditerator> forwarditerator unique (forwarditerator first, ForwardIterator Last){if(first== Last)return  Last; ForwardIterator result = first; while(++first! = Last)  {if(! (*result==*first))  //or:if(!pred (*result,*first)) forVersion (2)*(++result) =*first; }return++result;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leetcode26: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.