Leetcode (80): Delete duplicates in sorted array II

Source: Internet
Author: User
Tags repetition

medium!

Title Description:

Given a sorted array, you need to delete the repeating element in place so that each element appears up to two times, returning the new length of the array after it is removed.

Instead of using extra array space, you must modify the input array in place and complete it with O (1) Extra space.

Example 1:

 5 nums 1, 1, 2, 2, 3. You do not need to consider elements that are beyond the new length in the array.

Example 2:

nums 7 , and the first five elements of the original array are modified to be 0 , 0, 1, 1, 2, 3, 3. You do not need to consider elements that are beyond the new length in the array.

Description

Why is the return value an integer, but the answer to the output is an array?

Note that the input array is passed in a "reference" manner , which means that modifying the input array in the function is visible to the caller.

You can imagine the internal operation as follows:

The nums is passed in a "reference" manner. That is, do not make any copy of the argument int len = removeduplicates (nums);//Modifying an input array in a function is visible to the caller. Depending on the length returned by your function, it prints all the elements within that length range in the array. for (int i = 0; i < len; i++) {print (nums[i]);}

Problem Solving Ideas:

This allows the maximum number of repetitions is two, then we need to use a variable count to record also allow a few repetitions, count is initialized to 1, if there is a repetition, then count Decrements 1, then the next occurrence of repetition, fast pointer directly before further, if this is not repeated, Then count recovers 1, because the entire array is ordered, so if there are no duplicates, it must be larger than this number, and there will be no duplicates after this number. Clear up the above ideas, the code is very good to write.

C + + Solution One:

1 classSolution {2  Public:3     intRemoveDuplicates (intA[],intN) {4         if(N <=2)returnN;5         intPre =0, cur =1, Count =1;6          while(Cur <N) {7             if(A[pre] = = A[cur] && count = =0) ++cur;8             Else {9                 if(A[pre] = = A[cur])--count;Ten                 ElseCount =1; OneA[++pre] = a[cur++]; A             } -         } -         returnPre +1; the     } -};

Leetcode (80): Delete duplicates in 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.