Leetcode-Remove duplicates from Sorted Array

Source: Internet
Author: User

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 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],

Test instructions
The sorted string is removed repeatedly and returned to the length of the weight. Requires that no secondary space be used.

Ideas:
Define two iterators, equal to erase, unequal cnt++, and note the failure of iterators after deletion.

Code:
classSolution { Public:intRemoveDuplicates ( vector<int>& Nums) {if(nums.size () = =0){return 0; }if(nums.size () = =1){return 1; }AutoIter2 = Nums.begin ();intCNT =1; for(Autoiter = Nums.begin () +1; Iter! = Nums.end (); ){if(*iter2! = *iter)                {cnt++;                Iter2 = iter;            ++iter; }Else{iter = Nums.erase (ITER); }        }returnCnt }};

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.