Leetcode Remove duplicates from Sorted array removes duplicate elements from an integer array and returns the number of remaining elements

Source: Internet
Author: User

1 classSolution {2  Public:3 intRemoveDuplicates (intA[],intN) {4     int*s=&a[0],*e=&a[0];//s points at the beginning of the first, e traverses backwards the same5     intt,i,j=N;6      for(i=1; i<n;i++){7e++;8         if(*s==*e)9j--;Ten         Else{ Ones++; A*s=*e; -         } -     } the     returnJ; - } -};

Test instructions: Give an ordered array of integers, delete the duplicate elements, leave only one of several identical elements, and return the total number of different elements.

Idea: This is an array, so there is a repetition of the place to move the following elements, with two pointers s and e,e used to traverse all the elements, s to point to the currently processed place, eventually e will traverse to Nth, S will point to the last element of the new array.

Note: There's nothing to be careful of, so short code. Do not cross the pointer.

Leetcode Remove duplicates from Sorted array removes duplicate elements from an integer array and returns the number of remaining elements

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.