Remove duplicates from the sorted array

Source: Internet
Author: User

Given an ordered array, you need to delete the duplicates in place so that each element appears only once and returns the new length.

Do not define an array separately, you must do this by modifying the input array in place with O (1) extra memory.

Personal code, more mentally retarded.

Class Solution {
Public
int RemoveDuplicates (vector<int>& nums) {
Vector<int>::iterator ITER;
int m;
if (Nums.size () ==0) return 0;
else m=nums[0];
For (Iter=++nums.begin (); Iter!=nums.end ();)
{
if (*iter!=m) {
M=*iter;
++iter;
}
else{

Iter=nums.erase (ITER);

}
}
return int (nums.size ());
}
};

Ranked first code:

The idea is simply to iterate through the array, with different elements in advance, without changing the size of the array.

Class Solution {

Public :

int removeduplicates (vector<int>& nums) { /span>

short int Endpos = 0;

if (nums.size () = = 0) return 0;

for (short int i = 0; I<nums.size (); ++i) {

if (Nums[endpos]!=nums[i]) {

nums[++endpos] = Nums[i];

"

"

return endpos +1;

}

};

Remove duplicates from the 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.