# 80.Remove Duplicates from Sorted Array II --- ## 題目 * Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length. * Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. --- ## 我的作法 因為題目已經給定是排序過後的陣列,所以從Array的Index 0開始往後檢查,每次檢查是否為已出現過的數字,若不是則將該數字放置欲放置的index(該變數為expect),若為已出現過的數字,則再檢查是否只出現兩次以下(該變數為count),若是才將該變數放置欲放置的index。 --- ## C code: ```C= int removeDuplicates(int* nums, int numsSize) { int count = 0; int previous = nums[0]; int expect = 0; for(int i = 0;i < numsSize;i++) { if(nums[i] != previous) { nums[expect] = nums[i]; previous = nums[i]; expect++; count = 1; } else { if(count < 2) { nums[expect] = nums[i]; previous = nums[i]; expect++; count++; } } } return expect; } ``` ## 成績 ![](https://upload.cc/i1/2020/07/27/3S1rMv.png)