【題目描述】
Follow up for "Remove Duplicates":
What if duplicates are allowed at mosttwice?
For example,
Given sorted array A =[1,1,1,2,2,3],
Your function should return length =5, and A is now[1,1,2,2,3].
跟進(jìn)“刪除重復(fù)數(shù)字”:
如果可以允許出現(xiàn)兩次重復(fù)將如何處理?
【題目鏈接】
www.lintcode.com/en/problem/remove-duplicates-from-sorted-array-ii/
【題目解析】
限制條件:元素最多可重復(fù)出現(xiàn)兩次。需要有一變量跟蹤元素重復(fù)出現(xiàn)的次數(shù),小于指定值時(shí)執(zhí)行賦值操作。但是要注意的是重復(fù)出現(xiàn)次數(shù)occurence的初始值(從1開始,而不是0)和reset的時(shí)機(jī)。核心思想是兩指針,只不過此時(shí)新索引自增的條件是當(dāng)前遍歷的數(shù)組值和“新索引”或者“新索引-1”兩者之一不同。
【參考答案】
www.jiuzhang.com/solutions/remove-duplicates-from-sorted-array-ii/