Lintcode101 Remove Duplicates From Sorted Array || solution 题解

xiaoxiao2021-02-28  72

【题目描述】

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].

跟进“删除重复数字”:

如果可以允许出现两次重复将如何处理?

【题目链接】

www.lintcode.com/en/problem/remove-duplicates-from-sorted-array-ii/

【题目解析】

限制条件:元素最多可重复出现两次。需要有一变量跟踪元素重复出现的次数,小于指定值时执行赋值操作。但是要注意的是重复出现次数occurence的初始值(从1开始,而不是0)和reset的时机。核心思想是两指针,只不过此时新索引自增的条件是当前遍历的数组值和“新索引”或者“新索引-1”两者之一不同。

【参考答案】

www.jiuzhang.com/solutions/remove-duplicates-from-sorted-array-ii/

转载请注明原文地址: https://www.6miu.com/read-41869.html

最新回复(0)