Lintcode100 Remove Duplicates From Sorted Array solution 题解

【题目描述】

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度。

不要使用额外的数组空间,必须在原地没有额外空间的条件下完成。

【题目链接】

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

【题目解析】

首先我们需要知道,对于一个排好序的数组来说,A[N + 1] >= A[N],我们仍然使用两个游标i和j来处理,假设现在i = j + 1,如果A[i] == A[j],那么我们递增i,直到A[i] != A[j],这时候我们再设置A[j + 1] = A[i],同时递增i和j,重复上述过程直到遍历结束。

【参考答案】

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容