Leetcode------Remove Duplicates from Sorted Array II

Source: Internet
Author: User
Tags repetition

Title: Remove duplicates from Sorted Array II
Pass Rate: 30.7%
Difficulty: Medium

Ollow up for "Remove duplicates":
What if duplicates is allowed at the most twice?

For example,
Given sorted Array A = [1,1,1,2,2,3] ,

Your function should return length = 5 , and A is now [1,1,2,2,3] .

Made the first version of it. is to remove the repetition, and use a pointer to record where the real should be.

Now do remove more than 2 of the repetition, still with 1, there is a difference is to use a flag bit to determine whether it is greater than 2 on the line

See the code specifically:

1  Public classSolution {2      Public intRemoveDuplicates (int[] A) {3         if(a.length==0)return0;4         intIndex=1;5         intFlag=0;6          for(inti=1;i<a.length;i++){7             if(a[i]!=a[i-1]){8a[index]=A[i];9index++;TenFlag=0; One             } A             Else if(a[i]==a[i-1]){ -                 if(flag!=2){ -a[index]=A[i]; theindex++; -flag=2; -                 } -             } +         } -         returnindex; +     } A}

Leetcode------Remove Duplicates from Sorted Array II

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.