26. Remove Duplicates from Sorted Array★

来源:互联网 发布:python接口自动化测试 编辑:程序博客网 时间:2024/06/05 21:14

题目内容:

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

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

For example,
Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements ofnums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.

 

题目分析:注意数组是已经经过排序的数组,可以使用两个指针来对不重复的数字个数进行甄别。其中一个指针扫描数组的每一个元素,另外一个指针用来改变数组中的值,是的最终这个指针只想不同元素的最后一个位置。

 

题目代码:

 

0 0