[LeetCode] Remove Duplicates from Sorted Array II

Source: Internet
Author: User

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [,], Your function shocould return length = 5, and A is now [, 3]. problem description: similar to Remove Duplicates, duplicate elements in the sorted array need to be deleted. However, there is another condition: Each element can have up to two times, as shown in the preceding example. In fact, the method is the same as above. Use a variable to traverse the array and use another variable to point to the array that has been processed to meet the conditions. here we need to introduce another variable to save the count.

class Solution {  public:      int removeDuplicates(int A[], int n) {          // IMPORTANT: Please reset any member data you declared, as          // the same Solution instance will be reused for each test case.          int i = 0, j = 0, cnt = 0;                    if(n == 0 || n == 1)              return n;                    for(i = 0; i < n-1; ++i) {              if(A[i] == A[i+1] && cnt < 2) {                  A[j++] = A[i];                  cnt++;              }              else if(A[i] == A[i+1] && cnt >= 2) {                  cnt++;              }              else if(A[i] != A[i+1] && cnt < 2) {                  A[j++] = A[i];                  cnt = 0;              }              else if(A[i] != A[i+1] && cnt >= 2) {                  cnt = 0;              }          }                    if(A[n-1] != A[n-2] || A[n-1] == A[n-2] && cnt < 2)              A[j++] = A[n-1];                    return j;      }  };  

 

We will discuss the classification of the above four cases. When the current element is equal to the next element and the count is less than 2, we will add the current element to the array and add the Count value to 1; when the current element is equal to the next element and the count is greater than or equal to 2, the current element is not added to the array, but the Count value is added to 1. In fact, nothing can be done here; when the current element and the next element are not equal and the count is less than 2, add the current element to the array and clear the Count value to 0. When the current element and the next element are not listed and the count is greater than 2, clear the Count value to 0. Here we need to consider the boundary: the last element does not have the next element, so the last element needs to be taken out separately and A [N-2] is used when discussing the last element. Therefore, here we need to take out n = 0 and 1.

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.