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