Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.
class Solution {
public:
int removeDuplicates(int A[], int n) {
if (n == 0) return 0;
int index = 0;
for (int i = 1; i < n; i++)
{
if (A[index] != A[i])
A[++index] = A[i];
}
return index + 1;
}
};
这是一道关于线性表操作的简单题,考察的是递推思想,当然暴力查找也可以,但是复杂度不能满足要求:;
线性表的题型除了数组还有单链表、双向链表(我会尽量把提醒全写一次)