问题描述:
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 by modifying the input array in-place with O(1) extra memory.
代码如下 13ms
int removeDuplicates(int* nums, int numsSize) { if(numsSize == 0) { return 0; } int length = 1; for(int i = 0;i<numsSize -1; ++i) { if(nums[i] != nums[i + 1]) { nums[length++] = nums[i + 1]; } } return length; }