Leetcode first _ Remove Duplicates from Sorted Array II

Source: Internet
Author: User

Question.

I have said that it is difficult to include at least a few questions. You can repeat at a maximum of two times to repeat the task. Otherwise, you can simply move forward based on the number of repetitions, just like normal data. However, this inspace idea is still useful. The fatal disadvantage of array implementation is that it is too costly to delete or add Intermediate elements, because it is difficult to grasp the final position of data. This is a case. merging two sorted arrays is also an example.

Class Solution {public: int removeDuplicates (int A [], int n) {int past = A [0], due = 0, tpdue = 1; for (int I = 1; I <n; I ++) {if (A [I] = past) {tpdue ++; if (tpdue> 2) {due ++ ;} else {A [I-due] = A [I] ;}} else {past = A [I]; A [I-due] = A [I]; tpdue = 1 ;}}return n-due ;}};


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.