難度:medium
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
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.
此類題肯定是要用到hashtable,key對應的value是該數字出現的次數,當出現的次數超過兩次,(注意順序)先將value降至2,再刪除原list中的element,最後修改while語句中的n。