Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice?
For example:
1
2
3
Given sorted array nums = [1,1,1,2,2,3],
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.
建議可以先參考先前Remove Duplicates from Sorted Array的解法,解說較為詳細,基本上概念完全一樣,欲刪除一陣列中超過2個以上多餘的重覆值,可先遍歷一次對照前後是否相同,如果相同再判斷是否已經重覆超過2個以上,並一邊算出最後陣列的長度多少個(超過2個重覆就只以2個計算),同時將超過2個以上多餘的相同值賦予標示,例如極大值後做簡易選擇交換便能達成目標。